table 动态加载,遍历子控件

来源:互联网 发布:王者荣耀墨子知乎 编辑:程序博客网 时间:2024/06/05 02:56

<div id="DIV1" runat="server">
        <asp:Panel ID="panel1" runat="server">
        </asp:Panel>
        <asp:Button ID="Button3" runat="server" OnClick="Button3_Click" Text="Button" />
        <asp:TextBox ID="TextBox9" runat="server"></asp:TextBox>
     </div>

 

 

protected void Page_Load(object sender, EventArgs e)
    {
               
        Table table = new Table();
        panel1.Controls.Add(table);
        table.BorderWidth = 2;
        TableRow tr1 = new TableRow();
        TableRow tr2 = new TableRow();
        table.Rows.Add(tr1);
        table.Rows.Add(tr2);
        for (int i = 0; i < 3; i++)
        {
            TableCell tc1 = new TableCell();
            TableCell tc2 = new TableCell();
            tr1.Cells.Add(tc1);
            tr2.Cells.Add(tc2);

            TextBox textbox = new TextBox();
            Label lab = new Label();
            tc1.Controls.Add(lab);
            tc2.Controls.Add(textbox);
           
            lab.Text = " ghfg";

            textbox.Width = 80;
            string textboxID = "textbox" + i;
            textbox.ID = textboxID;
          
          
        }
       
    }

    protected void Button3_Click(object sender, EventArgs e)
    {        
       
        Table table = panel1.Controls[1] as Table;
        foreach (TableRow row in table.Rows)
        {
            foreach (TableCell cell in row.Cells)
            {
                foreach (Control control in cell.Controls)
                {
                    if (control is TextBox)
                    {
                        TextBox9.Text += ((TextBox)control).Text;
                    }
                }
            }
        }
    }

原创粉丝点击