在Repeater中设置Items的ID
在我的aspx中,我有一个包含三个文本框的转发器:
<asp:TextBox ID="myTextBox" runat="server"
在我的代码隐藏中,我将我的转发器int data = new int[3];
绑定到一个数组int data = new int[3];
所以我的页面显示了三个文本框,每个文本框的myTextBox ID都是三次。 有没有办法将这些ID设置为:
- MyTextBox1
- MyTextBox2
- MyTextBox3
所以我的页面显示了三个文本框,每个文本框的myTextBox ID都是三次。
你确定吗? 听起来你在谈论渲染输出。 查看来源,您会发现:
从后面的代码中,您可以通过ClientID
属性访问此生成的ID。 您还可以通过搜索转发器的Items
属性来访问各个控件:
TextBox textBox2 = myRepeater.Items[1].FindControl("myTextBox");
编辑:您可以显式设置控件的ClientID
。 您必须将其ClientIDMode
设置为Static
并在数据绑定时更改ID:
protected void Page_Load(object sender, EventArgs e) { myRepeater.ItemDataBound += new RepeaterItemEventHandler(myRepeater_ItemDataBound); myRepeater.DataSource = new int[3]; myRepeater.DataBind(); } void myRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e) { var textbox = e.Item.FindControl("myTextBox"); textbox.ClientIDMode = ClientIDMode.Static; textbox.ID = "myTextBox" + (e.Item.ItemIndex + 1); }
给出这个HTML: