Asp.Net 服务器控件动态生成及取值

protected void Page_Load(object sender, EventArgs e)
        {
            CreateControl(10);
        }
        private void CreateControl(int number)
        {
            for (int i = 0; i <= number; ++i)
            {
                TableRow tr = new TableRow();
                TableCell tcleft = new TableCell();
                Label lbltitle = new Label();
                lbltitle.Text = "Number:";
                TextBox txtpc = new TextBox();
                int numb = (i);
                txtpc.Text = i.ToString();
                tcleft.Controls.Add(lbltitle);
                tcleft.Controls.Add(txtpc);
                tr.Cells.Add(tcleft);
                Table1.Rows.Add(tr);
            }
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            foreach (TableRow tr in this.Table1.Rows)
            {
                foreach (TableCell tc in tr.Cells)
                {
                    if (tc.Controls[1] is TextBox)
                    {
                        TextBox txtbox = (tc.Controls[1] as TextBox);
                        string txtboxvalue = txtbox.Text;
                        Response.Write(txtboxvalue);
                    }
                }
            }
        }

image

posted @ 2011-03-07 03:50  Jon.Zhiwei@hotmail.com  Views(672)  Comments(2)    收藏  举报