Bewerken

Share via


UpdatePanel.CreateContentTemplateContainer Method

Definition

Creates a Control object that acts as a container for child controls that define the UpdatePanel control's content.

protected:
 virtual System::Web::UI::Control ^ CreateContentTemplateContainer();
protected virtual System.Web.UI.Control CreateContentTemplateContainer ();
abstract member CreateContentTemplateContainer : unit -> System.Web.UI.Control
override this.CreateContentTemplateContainer : unit -> System.Web.UI.Control
Protected Overridable Function CreateContentTemplateContainer () As Control

Returns

A Control container for the UpdatePanel control's content.

Examples

The following example shows how to override the CreateContentTemplateContainer method in a custom UpdatePanel control to always render the <fieldset> and <legend> elements for the panel's content. The custom UpdatePanel control defines a public property named GroupingText that is a string literal inside the <legend> element.

A custom UpdatePanel control named CustomUpdatePanel derives from UpdatePanel and overrides the CreateContentTemplateContainer method. Put the CustomUpdatePanel class source code in the Web site's App_Code folder.

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SamplesCS
{
    public class CustomUpdatePanel : System.Web.UI.UpdatePanel
    {
        public CustomUpdatePanel()
        {
        }

        private String _groupingText;
        public String GroupingText
        {
            get { return _groupingText; }
            set { _groupingText = value; }
        }

        protected override Control CreateContentTemplateContainer()
        {
            MyContentTemplateContainer myContentTemplateContainer =
                new MyContentTemplateContainer(_groupingText);
            return myContentTemplateContainer;
        }
        private sealed class MyContentTemplateContainer : Control
        {
            private String _displayText;
            public MyContentTemplateContainer(string groupingText)
            {
                _displayText = groupingText;
            }
            protected override void Render(HtmlTextWriter writer)
            {
                writer.RenderBeginTag(HtmlTextWriterTag.Fieldset);
                writer.RenderBeginTag(HtmlTextWriterTag.Legend);
                writer.Write(_displayText);
                writer.RenderEndTag();
                base.Render(writer);
                writer.RenderEndTag();
            }
        }
    }
}
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls

Namespace SamplesVB
    Public Class CustomUpdatePanel : Inherits System.Web.UI.UpdatePanel

        Public CustomUpdatePanel()

        Private _groupingText As String

        Public Property GroupingText() As String
            Get
                Return _groupingText
            End Get
            Set(ByVal value As String)
                _groupingText = value
            End Set
        End Property

        Protected Overrides Function CreateContentTemplateContainer() As Control
            Dim myContentTemplateContainer As MyContentTemplateContainer
            myContentTemplateContainer = New MyContentTemplateContainer(_groupingText)
            Dim myControl As Control
            myControl = myContentTemplateContainer
            Return myControl
        End Function

        Private NotInheritable Class MyContentTemplateContainer : Inherits Control

            Private _displayText As String
            Public Sub New(ByVal groupingText As String)
                _displayText = groupingText
            End Sub

            Protected Overrides Sub Render(ByVal writer As HtmlTextWriter)

                writer.RenderBeginTag(HtmlTextWriterTag.Fieldset)
                writer.RenderBeginTag(HtmlTextWriterTag.Legend)
                writer.Write(_displayText)
                writer.RenderEndTag()
                MyBase.Render(writer)
                writer.RenderEndTag()
            End Sub

        End Class
    End Class
End Namespace

The custom UpdatePanel control is used on the page just as the UpdatePanel control is. The following example shows a page that contains the custom UpdatePanel control.

<%@ Page Language="C#" %>
<%@ Register Namespace="SamplesCS" TagPrefix="Samples" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>CreateContentTemplateContainer Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ScriptManager ID="ScriptManager1"
                           runat="server" />
        <Samples:CustomUpdatePanel ID="UpdatePanel1"
                                   UpdateMode="Conditional"
                                   GroupingText="This is an UpdatePanel."
                                   runat="server">
            <ContentTemplate>
                <asp:Calendar ID="Calendar1"
                              runat="server" />
            </ContentTemplate>
        </Samples:CustomUpdatePanel>
    </div>
    </form>
</body>
</html>
<%@ Page Language="VB" %>
<%@ Register Namespace="SamplesCS" TagPrefix="Samples" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>CreateContentTemplateContainer Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ScriptManager ID="ScriptManager1"
                           runat="server" />
        <Samples:CustomUpdatePanel ID="UpdatePanel1"
                                   UpdateMode="Conditional"
                                   GroupingText="This is an UpdatePanel."
                                   runat="server">
            <ContentTemplate>
                <asp:Calendar ID="Calendar1"
                              runat="server" />
            </ContentTemplate>
        </Samples:CustomUpdatePanel>
    </div>
    </form>
</body>
</html>

Remarks

This method is intended for use by control developers who want to extend the UpdatePanel control. For example, in derived classes, you can provide a different root control that acts as a container for your UpdatePanel control's content. The default implementation returns a Control object.

Applies to

See also