Building Hybrid Controls : CompositeControl « Custom Controls « ASP.NET Tutorial






File: Login.cs

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace myControls
{
    public class Login : CompositeControl
    {
        private TextBox txtUserName;
        private TextBox txtPassword;

        public string UserName
        {
            get
            {
                EnsureChildControls();
                return txtUserName.Text;
            }

            set
            {
                EnsureChildControls();
                txtUserName.Text = value;
            }
        }

        public string Password
        {
            get
            {
                EnsureChildControls();
                return txtPassword.Text;
            }

            set
            {
                EnsureChildControls();
                txtPassword.Text = value;
            }
        }

        protected override void CreateChildControls()
        {
            txtUserName = new TextBox();
            txtUserName.ID = "txtUserName";
            this.Controls.Add(txtUserName);

            txtPassword = new TextBox();
            txtPassword.ID = "txtPassword";
            txtPassword.TextMode = TextBoxMode.Password;
            this.Controls.Add(txtPassword);
        }

        protected override void RenderContents(HtmlTextWriter writer)
        {
            writer.RenderBeginTag(HtmlTextWriterTag.Tr);

            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.AddAttribute(HtmlTextWriterAttribute.For, txtUserName.ClientID);
            writer.RenderBeginTag(HtmlTextWriterTag.Label);
            writer.Write("User Name:");
            writer.RenderEndTag(); // Label
            writer.RenderEndTag(); // TD

            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            txtUserName.RenderControl(writer);
            writer.RenderEndTag(); // TD

            writer.RenderEndTag();
            writer.RenderBeginTag(HtmlTextWriterTag.Tr);

            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.AddAttribute(HtmlTextWriterAttribute.For, txtPassword.ClientID);
            writer.RenderBeginTag(HtmlTextWriterTag.Label);
            writer.Write("Password:");
            writer.RenderEndTag(); // Label
            writer.RenderEndTag(); // TD

            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            txtPassword.RenderControl(writer);
            writer.RenderEndTag(); // TD

            writer.RenderEndTag(); // TR
        }

        protected override HtmlTextWriterTag TagKey
        {
            get
            {
                return HtmlTextWriterTag.Table;

            }
        }

    }
}








14.20.CompositeControl
14.20.1.Building Composite Controls
14.20.2.Building Hybrid Controls
14.20.3.Performing layout with an HTML table.
14.20.4.Item Rotator
14.20.5.Image Rotator
14.20.6.Creating a Default Template
14.20.7.File: Product.cs
14.20.8.Supporting Two-Way Databinding
14.20.9.Creating Templated Databound Controls