读书人

怎么获取动态生成的id相同的textbox服

发布时间: 2012-12-17 09:31:40 作者: rapoo

如何获取动态生成的id相同的textbox服务器控件的值
http://bbs.csdn.net/topics/390297553?page=1#post-393047312

如此贴,前台一表格,用repeater动态生成。使用三楼的方法,改为:
<asp:TextBox ID="txtName" runat="server" Text='<%#Eval("fieldName")%>'></asp:TextBox>
动态生成了textbox,只是这样生成的ID都是一样的,在后台如何取值呢?

程序整体结构参见6楼。

[最优解释]
var tb = Repeater2.Items[0].FindControl("txtName") as TextBox;
[其他解释]
<asp:Repeater ID="propertyList" runat="server">
<ItemTemplate>
<tr>
<td style="width: 30%; height: 25px; background-color: #ffffff;text-align:left">
     <%# Eval("propertyName") %>
</td>
<td style="text-align: left; background-color: #ffffff;">
<asp:TextBox ID="txtName" runat="server" Text='<%#Eval("fieldName")%>'></asp:TextBox>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
[其他解释]
是从.cs文件中获取吗?
[其他解释]

引用:
var tb = Repeater2.Items[0].FindControl("txtName") as TextBox;


请问这是在后台获取吗?.cs文件中?
[其他解释]
asp:Repeater 用来显示数据,为啥还要后台取值。

如果要取得话,可以遍历asp:Repeater 的行,使用FindControl("ID")来获取每行的asp:TextBox
[其他解释]
protected void propertyList_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item
[其他解释]
e.Item.ItemType ==ListItemType.AlternatingItem)
{
Textbox Textbox = (Textbox)e.Item.FindControl("txtName");
string str= Textbox.Text;
}
}
[其他解释]
不好意思,才看到后面两位的回复。已按第一位回贴的高人所示,解决问题。辛苦后面两位了。谢谢。

读书人网 >asp.net

热点推荐