WebCustomControl1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication2
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:WebCustomControl1 runat=server></{0}:WebCustomControl1>")] 
    public class WebCustomControl1 : WebControl
    {
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Text
        {
            get
            {
                String s = (String)ViewState["Text"];
                return ((s == null) ? "["+this.ID+"]" : s);
            }

            set
            {            
                if (txtbl != null)
                {
                    this.Text = value;
                }
                else {
                    ViewState["Text"] = value;    
                }
            }
        }

        protected override void RenderContents(HtmlTextWriter output)
        {
            this.EnsureChildControls(); //하위 컨트롤 찾음CreateChildControls() 실행
            output.WriteBeginTag("table");
            output.Write(">");
            output.WriteBeginTag("tr");
            output.Write(">");
            output.WriteBeginTag("td");
            output.Write(">");
txtbl.RenderControl(output); //하위컨트롤 뿌려줌

            output.WriteEndTag("td");
            output.WriteEndTag("tr");
            output.WriteEndTag("table");
        }
        private TextBox txtbl;
        protected override void CreateChildControls()
        {
            txtbl = new TextBox();
            this.Controls.Add(txtbl);
        }

    }
}

+ Recent posts