ASP.NET Tutorial/Custom Controls/TemplateContainer

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Adding a templated control to a Web page (C#)

<%@ Page Language="C#" %>
<%@ Register Assembly="WebControlLibrary1" Namespace="WebControlLibrary1" 
    TagPrefix="cc1" %>
<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        this.TemplatedControl1.DataBind();
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Templated Web Controls</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <cc1:TemplatedControl Name="John Doe" Text="Hello World!" 
            ID=" TemplatedControl1" runat="server">
            <MessageTemplate>The user "<%# Container.Name %>" 
                has a message for you: <br />"<%#Container.Text%>"
            </MessageTemplate>
        </cc1:TemplatedControl>        
    </div>
    </form>
</body>
</html>
File: TemplatedControl.cs

using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebControlLibrary1
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:TemplatedControl runat=server></{0}: TemplatedControl >")]
    public class TemplatedControl : WebControl
    {
        private string _name;
        private string _text;
        private Message _message;
        private ITemplate _messageTemplate;
        [Browsable(false)]
        public Message Message
        {
            get
            {
                EnsureChildControls();
                return _message;
            }
        }
        [PersistenceMode(PersistenceMode.InnerProperty)]
        [TemplateContainer(typeof(Message))]
        public virtual ITemplate MessageTemplate
        {
            get { return _messageTemplate; }
            set { _messageTemplate = value; }
        }
        [Bindable(true)]
        [DefaultValue("")]
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }
        [Bindable(true)]
        [DefaultValue("")]
        public string Text
        {
            get { return _text; }
            set { _text = value; }
        }
        public override void DataBind()
        {
            CreateChildControls();
            ChildControlsCreated = true;
            base.DataBind();
        }
        protected override void CreateChildControls()
        {
            this.Controls.Clear();
            _message = new Message(Name, Text);
            ITemplate template = MessageTemplate;
            template.InstantiateIn(_message);
            Controls.Add(_message);
        }
    }
}


Adding a templated control to a Web page (VB)

<%@ Page Language="VB" %>
<%@ Register Assembly="WebControlLibrary1" Namespace="WebControlLibrary1" 
    TagPrefix="cc1" %>
<script runat="server">
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        Me.TemplatedControl1.DataBind()
    End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Templated Web Controls</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <cc1:TemplatedControl Name="John Doe" Text="Hello World!" 
            ID=" TemplatedControl1" runat="server">
            <MessageTemplate>The user "<%# Container.Name %>" 
                has a message for you: <br />"<%#Container.Text%>"
            </MessageTemplate>
        </cc1:TemplatedControl>        
    </div>
    </form>
</body>
</html>

File: TemplatedControl.vb

Imports System.ruponentModel
Imports System.Web.UI
Imports System.Web.UI.WebControls
<DefaultProperty("Text")> _
<ToolboxData("<{0}:TemplatedControl runat=server></{0}:TemplatedControl>")> _
Public Class TemplatedControl
    Inherits System.Web.UI.WebControls.WebControl
    Private _name As String
    Private _text As String
    Private _message As Message
    Private _messageTemplate As ITemplate
    <Browsable(True)> Public ReadOnly Property Message() As Message
        Get
            EnsureChildControls()
            Return _message
        End Get
    End Property
    <PersistenceMode(PersistenceMode.InnerProperty), _
        TemplateContainer(GetType(Message))> _
    Public Property MessageTemplate() As ITemplate
        Get
            Return _messageTemplate
        End Get
        Set(ByVal value As ITemplate)
            _messageTemplate = value
        End Set
    End Property
    <Bindable(True), DefaultValue("")> Public Property Name() As String
        Get
            Return _name
        End Get
        Set(ByVal value As String)
            _name = value
        End Set
    End Property
    <Bindable(True), DefaultValue("")> Public Property Text() As String
        Get
            Return _text
        End Get
        Set(ByVal value As String)
            _text = value
        End Set
    End Property
    Public Overrides Sub DataBind()
        CreateChildControls()
        ChildControlsCreated = True
        MyBase.DataBind()
    End Sub
    Protected Overrides Sub CreateChildControls()
        Me.Controls.Clear()
        _message = New Message(Name, Text)
        Dim template As ITemplate = MessageTemplate
        template.InstantiateIn(_message)
        Controls.Add(_message)
    End Sub
End Class


Creating the template control container class (C#)

using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebControlLibrary1
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:TemplatedControl runat=server></{0}: TemplatedControl >")]
    public class TemplatedControl : WebControl
    {
        private string _name;
        private string _text;
        private Message _message;
        private ITemplate _messageTemplate;
        [Browsable(false)]
        public Message Message
        {
            get
            {
                EnsureChildControls();
                return _message;
            }
        }
        [PersistenceMode(PersistenceMode.InnerProperty)]
        [TemplateContainer(typeof(Message))]
        public virtual ITemplate MessageTemplate
        {
            get { return _messageTemplate; }
            set { _messageTemplate = value; }
        }
        [Bindable(true)]
        [DefaultValue("")]
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }
        [Bindable(true)]
        [DefaultValue("")]
        public string Text
        {
            get { return _text; }
            set { _text = value; }
        }
        public override void DataBind()
        {
            CreateChildControls();
            ChildControlsCreated = true;
            base.DataBind();
        }
        protected override void CreateChildControls()
        {
            this.Controls.Clear();
            _message = new Message(Name, Text);
            ITemplate template = MessageTemplate;
            template.InstantiateIn(_message);
            Controls.Add(_message);
        }
    }
}


Creating the template control container class (VB)

Imports System.ruponentModel
Imports System.Web.UI
Imports System.Web.UI.WebControls
<DefaultProperty("Text")> _
<ToolboxData("<{0}:TemplatedControl runat=server></{0}:TemplatedControl>")> _
Public Class TemplatedControl
    Inherits System.Web.UI.WebControls.WebControl
    Private _name As String
    Private _text As String
    Private _message As Message
    Private _messageTemplate As ITemplate
    <Browsable(True)> Public ReadOnly Property Message() As Message
        Get
            EnsureChildControls()
            Return _message
        End Get
    End Property
    <PersistenceMode(PersistenceMode.InnerProperty), _
        TemplateContainer(GetType(Message))> _
    Public Property MessageTemplate() As ITemplate
        Get
            Return _messageTemplate
        End Get
        Set(ByVal value As ITemplate)
            _messageTemplate = value
        End Set
    End Property
    <Bindable(True), DefaultValue("")> Public Property Name() As String
        Get
            Return _name
        End Get
        Set(ByVal value As String)
            _name = value
        End Set
    End Property
    <Bindable(True), DefaultValue("")> Public Property Text() As String
        Get
            Return _text
        End Get
        Set(ByVal value As String)
            _text = value
        End Set
    End Property
    Public Overrides Sub DataBind()
        CreateChildControls()
        ChildControlsCreated = True
        MyBase.DataBind()
    End Sub
    Protected Overrides Sub CreateChildControls()
        Me.Controls.Clear()
        _message = New Message(Name, Text)
        Dim template As ITemplate = MessageTemplate
        template.InstantiateIn(_message)
        Controls.Add(_message)
    End Sub
End Class