组项目的ID在一个中继器 [英] Set ID of Items In a Repeater
问题描述
在我的aspx,我有一个包含三个文本框的中继器:
< ASP:直放站ID =myRepeater=服务器>
<&ItemTemplate中GT;
< ASP:文本框ID =myTextBox=服务器
<的ItemTemplate />
< / ASP:直放站>
在我的codebehind,我有我的中继数据绑定到一个数组 int数据= INT新[3];
所以我的页面显示三个文本框,各有myTextBox三倍的ID。有没有一种方法来设置这些ID是:
- MyTextBox1
- MyTextBox2
- MyTextBox3
所以我的页面显示三个文本框,各有myTextBox三倍的ID。
块引用>您确定吗?这听起来像你正在谈论的渲染输出。查看源代码,你会发现:
<输入名称=$ myRepeater $ ctl00 myTextBox类型=文本ID =myRepeater_myTextBox_0/>
<输入名称=$ myRepeater $ ctl01 myTextBox类型=文本ID =myRepeater_myTextBox_1/>
<输入名称=$ myRepeater $ ctl02 myTextBox类型=文本ID =myRepeater_myTextBox_2/>从code的背后,你可以访问此产生通过
:您还可以通过你的中继的客户端ID
属性ID。项目
属性搜索访问各个控制文本框TextBox2中= myRepeater.Items [1] .FindControl(myTextBox);
编辑:您的可以的明确设置
客户端ID
的控制。你必须设置它的<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.ui.control.clientidmode.aspx\"><$c$c>ClientIDMode$c$c>到静态
并改变当它在数据绑定ID:保护无效的Page_Load(对象发件人,EventArgs的发送)
{
myRepeater.ItemDataBound + =新RepeaterItemEventHandler(myRepeater_ItemDataBound);
myRepeater.DataSource =新INT [3];
myRepeater.DataBind();
}无效myRepeater_ItemDataBound(对象发件人,RepeaterItemEventArgs E)
{
变种文本= e.Item.FindControl(myTextBox);
textbox.ClientIDMode = ClientIDMode.Static;
textbox.ID =myTextBox+(e.Item.ItemIndex + 1);
}给出了这样的HTML:
&LT;输入名称=$ myRepeater $ ctl01 myTextBox1类型=文本ID =myTextBox1/&GT;
&LT;输入名称=$ myRepeater $ ctl02 myTextBox2类型=文本ID =myTextBox2/&GT;
&LT;输入名称=$ myRepeater $ ctl02 myTextBox3类型=文本ID =myTextBox3/&GT;In my aspx, I have a repeater which contains three textboxes:
<asp:Repeater ID="myRepeater" runat="server"> <ItemTemplate> <asp:TextBox ID="myTextBox" runat="server" <ItemTemplate/> </asp:Repeater>
In my codebehind, I have my repeater databound to an array
int data = new int[3];
So my page displays three textboxes, each with the ID of myTextBox three times. Is there a way to set those IDs to be:
- MyTextBox1
- MyTextBox2
- MyTextBox3
解决方案So my page displays three textboxes, each with the ID of myTextBox three times.
Are you sure about that? It sounds like you are talking about the rendered output. View the source and you will find:
<input name="myRepeater$ctl00$myTextBox" type="text" id="myRepeater_myTextBox_0" /> <input name="myRepeater$ctl01$myTextBox" type="text" id="myRepeater_myTextBox_1" /> <input name="myRepeater$ctl02$myTextBox" type="text" id="myRepeater_myTextBox_2" />
From the code behind, you can access this generated id via the
ClientID
property. You can also access individual controls by searching through your repeater'sItems
property:TextBox textBox2 = myRepeater.Items[1].FindControl("myTextBox");
Edit: You can explicitly set the
ClientID
for a control. You have to set itsClientIDMode
toStatic
and change the ID when it is databound: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); }
Gives this HTML:
<input name="myRepeater$ctl01$myTextBox1" type="text" id="myTextBox1" /> <input name="myRepeater$ctl02$myTextBox2" type="text" id="myTextBox2" /> <input name="myRepeater$ctl02$myTextBox3" type="text" id="myTextBox3" />
这篇关于组项目的ID在一个中继器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!