日期:2014-05-17  浏览次数:20488 次

如何获取动态生成的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">
                           &nbsp;&nbsp;&nbsp;&nbsp; <%# 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;
}

------其他解决方案--------------------
不好意思,才看到后面两位的回复。已按第一位回贴的高人所示,解决问题。辛苦后面两位了。谢谢。