【发布时间】:2012-06-18 09:50:10
【问题描述】:
我通过按钮点击后面的代码动态添加了文本框,它工作正常。在另一个按钮上单击我想获取所有动态添加的文本框的值。但是文本框的值为空。如何在回发时保留动态添加的文本框的值?
aspx代码
<asp:Table runat="server" id="placeAddTds">
<asp:TableRow>
<asp:TableCell><asp:TextBox ID="txt_from" runat="server" class="sabsw" TabIndex="88"></asp:TextBox></asp:TableCell>
<asp:TableCell><asp:TextBox ID="txt_to" runat="server" class="sabsw" TabIndex="89"></asp:TextBox></asp:TableCell>
<asp:TableCell><asp:TextBox ID="txt_per" runat="server" class="sabsw" TabIndex="90"></asp:TextBox></asp:TableCell>
<asp:TableCell>
<asp:Button class="main-btn add-btn pointer" runat="server" ID="btn_add" Text="Add" OnClick="TdsAddClick" TabIndex="91"></asp:Button>
</asp:TableCell>
</asp:TableRow>
</asp:Table>
后面的代码:
protected void TdsAddClick(object sender,EventArgs e)
{
try
{
TextBox[] l1 = new TextBox[txtcount];
TextBox[] l2 = new TextBox[txtcount];
TextBox[] l3 = new TextBox[txtcount];
for (int j = 1; j < txtcount; j++)
{
l1[j] = new TextBox();
l2[j] = new TextBox();
l3[j] = new TextBox();
l1[j].ID = "txt_from" + j;
l1[j].Attributes.Add("class","sabsw");
l2[j].ID = "txt_to" + j;
l2[j].Attributes.Add("class", "sabsw");
l3[j].ID = "txt_per" + j;
l3[j].Attributes.Add("class", "sabsw");
placeAddTds.Rows.Add(new TableRow());
placeAddTds.Rows[j].Cells.Add(new TableCell());
placeAddTds.Rows[j].Cells[0].Controls.Add(l1[j]);
placeAddTds.Rows[j].Cells.Add(new TableCell());
placeAddTds.Rows[j].Cells[1].Controls.Add(l2[j]);
placeAddTds.Rows[j].Cells.Add(new TableCell());
placeAddTds.Rows[j].Cells[2].Controls.Add(l3[j]);
}
txtcount++;
}
catch (Exception ex)
{
}
}
谢谢,
【问题讨论】:
-
你必须在每次回发时重新创建它们并从表单集合中获取值
-
我认为它是在每次回发时创建的。但我不知道如何在另一个按钮的按钮单击时获取动态创建的文本框的值。
-
我认为这不是一个好方法,如果您发布您的请求,那么可以提供比这更好的方法。
-
我有两个按钮。单击一个按钮时,我想动态添加文本框,单击第二个按钮时,我想保存数据库中所有文本框的值。
-
看看方式可以避免回发。