日期:2014-05-17  浏览次数:20440 次

动态添加控件并添加事件

 protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (ViewState["Count"] != null)
                {
         
                    Addtr();
                        
                }
            }
        }
     
        public void Addtr()
        {
               for (int i = 0; i < Convert.ToInt32(ViewState["Count"]); i++)
                {
                    HtmlTableRow r = new HtmlTableRow();

                    HtmlTableCell tc = new HtmlTableCell();
                    tc.InnerText = "IP Address " + (i + 2).ToString();
                    HtmlTableCell tc1 = new HtmlTableCell();
                    TextBox tx = new TextBox();
                    tx.ID = tc.InnerText + "StartIP";
                    tc1.Controls.Add(tx);
                    HtmlTableCell tc2 = new HtmlTableCell();
                    TextBox tx1 = new TextBox();
                    tx1.ID = tc.InnerText + "EndIp";
                    tc2.Controls.Add(tx1);
                    HtmlTableCell tc3 = new HtmlTableCell();
                    LinkButton lk