控件开发--多控件的布局

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 MyControl
{
    [ToolboxData("<{0}:MyFirstControl runat=server></{0}:MyFirstControl>")]
    public class MyFirstControl : WebControl
    {
        private Label myLbl = new Label();
        private TextBox myTXT = new TextBox();
        private Button myBTN = new Button();
        private Panel pnl = new Panel();
        private event  EventHandler btnClick ;
        public MyFirstControl()
        {
      
            myBTN.Text = "点我啊";
            myBTN.Width = 150;
            myBTN.Height = 50;
            myBTN.BackColor = System.Drawing.Color.Green;
            this.Controls.Add(myLbl);
            this.Controls.Add(myTXT);
            this.Controls.Add(myBTN);
            this.myBTN.Click += new EventHandler(this.btn_Click);
        }

        protected override void RenderContents(HtmlTextWriter output)
        {
            this.pnl.RenderBeginTag(output);
            output.AddAttribute(HtmlTextWriterAttribute.Border, "0");
            output.AddAttribute(HtmlTextWriterAttribute.Border, "0");
            output.AddAttribute(HtmlTextWriterAttribute.Cellpadding, "0");
            output.AddAttribute(HtmlTextWriterAttribute.Cellspacing, "0");
            output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Width, "100%");
            output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Height, "100%");
            output.AddAttribute(HtmlTextWriterAttribute.Bgcolor, "Red");

            output.RenderBeginTag(HtmlTextWriterTag.Table);

            output.RenderBeginTag(HtmlTextWriterTag.Tr);

            output.RenderBeginTag(HtmlTextWriterTag.Td);

            this.myLbl.RenderControl(output);

            output.RenderEndTag();

            output.RenderBeginTag(HtmlTextWriterTag.Td);

            this.myTXT.RenderControl(output);

            output.RenderEndTag();

            output.RenderBeginTag(HtmlTextWriterTag.Td);
            this.myBTN.RenderControl(output);
            output.RenderEndTag();

            output.RenderEndTag();
            output.RenderEndTag();

            this.pnl.RenderEndTag(output);
        }
        private void btn_Click(object sender, EventArgs e)
        {
            this.myLbl.Text = "我触发了按钮事件!";
            EventArgs e1 = new EventArgs();
            if (this.btnClick != null)
            {
                this.btnClick(this.myBTN, e1);
            }
        }
    }
}

posted @ 2009-04-30 16:14  只想做好  阅读(168)  评论(0)    收藏  举报