0

我使用更新面板和脚本管理器向表格添加新行,每行有 4 个文本框。现在,如果已将它们添加到表格中,我该如何处理并使用它们!

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
                <asp:Table ID="Table5" runat="server" Width="100%">
                    <asp:TableHeaderRow>
                        <asp:TableHeaderCell ID="TableHeaderCell1" runat="server">1</asp:TableHeaderCell><asp:TableHeaderCell
                            ID="TableHeaderCell2" runat="server">2</asp:TableHeaderCell><asp:TableHeaderCell
                                ID="TableHeaderCell3" runat="server">3</asp:TableHeaderCell><asp:TableHeaderCell
                                    ID="TableHeaderCell4" runat="server">4</asp:TableHeaderCell></asp:TableHeaderRow>
                    <asp:TableRow>
                    </asp:TableRow>
                </asp:Table>
                <center>
                    <asp:ImageButton ID="ImageButton1" runat="server" OnClick="ImageButton1_Click"
                    ImageUrl="~/Images/add.jpg" ValidationGroup="AddSavabegh" /></center>
                <br />
            </ContentTemplate>
        </asp:UpdatePanel>

后面的代码:

   private void GenerateTable(int rowsCount)
    {
        const int colsCount = 4;
        for (int i = 0; i < rowsCount; i++)
        {
            TableRow row = new TableRow();
            for (int j = 0; j < colsCount; j++)
            {
                TableCell cell = new TableCell();
                TextBox tb = new TextBox();
                tb.ID = "txtSabeghe_" + (i+1) + "Col_" + (j+1);
                cell.Controls.Add(tb);
                row.Cells.Add(cell);
            }
            Table5.Rows.Add(row);
        }
        SetPreviousData(rowsCount, colsCount);
        rowsCount++;
        ViewState["RowsCount"] = rowsCount;
    }

我使用了这个,但它返回 null :

protected void Button1_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < 3; i++)
        {
            for (int j = 0; i < 4; i++)
            {
                TextBox tb = (TextBox)UpdatePanel1.FindControl("txtSabeghe_" + (i+1) + "Col_" + (j+1));
                Response.Write(tb.Text);
            }
        } 

    }
4

1 回答 1

0

GenerateTable在您之前回发 Button1 单击事件时调用FindControl()..

于 2012-09-24T07:30:07.893 回答