在c#和asp.net中更新动态创建的文本框

时间:2012-02-29 07:30:39

标签: c# asp.net dynamic textbox

我有asp.net web应用程序,其中我用来生成文本框列表,取决于表中的计数,并动态生成列结尾处的按钮,以更新文本框中的值。我附加了示例代码供参考,例如目的我已分配 c 限制为 5 如何在动态生成的按钮点击事件中更新此文本框控件。

示例代码:

TableRow rowpoint = new TableRow();
                    TableCell cellpoint = new TableCell();
                    cellpoint.Text = "Total Points";
                    rowpoint.Cells.Add(cellpoint);

                    for (int c = 0; c < 5; c++)
                    {

                        TableCell cellinput = new TableCell();
                        TextBox txtinput = new TextBox();

                        cellinput.Controls.Add(txtinput);
                        rowpoint.Cells.Add(cellinput);
                    }


                    TableCell totpoint = new TableCell();

                    totpoint.BackColor = System.Drawing.Color.FromArgb(221, 221, 221);

                    Button btnupdate2 = new Button();
                    btnupdate2.Text = "Update";
                    totpoint.Controls.Add(btnupdate2);
                    rowpoint.Cells.Add(totpoint);
                    tbl1.Rows.Add(rowpoint);
                    //End Adding Total Points

1 个答案:

答案 0 :(得分:0)

在创建文本框时为每个文本框指定唯一ID。在创建按钮时添加按钮单击处理程序

btnupdate2.Click += new System.EventHandler(this.btnupdate2_OnClick)

然后编写以下事件处理程序

protected void btnupdate2_OnClick(object sender, EventArgs e) {


}

在其中使用FindControl来获取每个文本框。