Performing layout with an HTML table. : CompositeControl « Custom Controls « ASP.NET Tutorial






File: LoginStandards.cs

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

namespace myControls
{
    public class LoginStandards : 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.AddStyleAttribute("float", "left");
            writer.RenderBeginTag(HtmlTextWriterTag.Div);
            writer.AddStyleAttribute(HtmlTextWriterStyle.Padding, "3px");
            writer.RenderBeginTag(HtmlTextWriterTag.Div);
            writer.AddAttribute(HtmlTextWriterAttribute.For, txtUserName.ClientID);
            writer.RenderBeginTag(HtmlTextWriterTag.Label);
            writer.Write("User Name:");
            writer.RenderEndTag();
            writer.RenderEndTag();

            writer.AddStyleAttribute(HtmlTextWriterStyle.Padding, "3px");
            writer.RenderBeginTag(HtmlTextWriterTag.Div);
            writer.AddAttribute(HtmlTextWriterAttribute.For, txtPassword.ClientID);
            writer.RenderBeginTag(HtmlTextWriterTag.Label);
            writer.Write("Password:");
            writer.RenderEndTag();
            writer.RenderEndTag();
            writer.RenderEndTag();

            writer.AddStyleAttribute("float", "left");
            writer.RenderBeginTag(HtmlTextWriterTag.Div);
            writer.AddStyleAttribute(HtmlTextWriterStyle.Padding, "3px");
            writer.RenderBeginTag(HtmlTextWriterTag.Div);
            txtUserName.RenderControl(writer);
            writer.RenderEndTag();

            writer.AddStyleAttribute(HtmlTextWriterStyle.Padding, "3px");
            writer.RenderBeginTag(HtmlTextWriterTag.Div);
            txtPassword.RenderControl(writer);
            writer.RenderEndTag();
            writer.RenderEndTag();

            writer.Write("<br style='clear:left' />");
        }

        protected override HtmlTextWriterTag TagKey
        {
            get
            {
                return HtmlTextWriterTag.Div;
            }
        }

    }
}








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