asp:Repeater - 如何制作“StringEnumerator” 移动到“ItemDataBound”中的下一个项目?
这是中继器代码的快照。 我在获取所有值时遇到问题。 结果我的“中继器”仅打印集合中的第一个元素。
<asp:Repeater ID="repeatAdministrators" OnItemDataBound="repeatAdministrators_ItemDataBound" runat="server">
<HeaderTemplate>
<tr>
<td class="formLabel">
Administrators:
</td>
<td class="formInputText">
</HeaderTemplate>
<ItemTemplate>
<asp:TextBox ID="txtAdministrators" runat="server" MaxLength="50" Enabled="False"></asp:TextBox><br />
</ItemTemplate>
<FooterTemplate>
</td> </tr>
<tr>
</FooterTemplate>
</asp:Repeater>
这里是我想要更改集合中的读取索引的代码。 AdminsEnumerator.MoveNext();
未按我的预期工作。 添加类似 int t; 的内容 我++; StringCollection[i]
很烦人。 有更简单的解决方案吗?
protected void repeatAdministrators_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
TextBox txtAdministrators = e.Item.FindControl("txtAdministrators") as TextBox;
StringEnumerator AdminsEnumerator = Computer.Administrators.GetEnumerator();
AdminsEnumerator.MoveNext();
txtAdministrators.Text = AdminsEnumerator.Current;
}
}
Here is the snapshot of repeater code. I have problems with geting all the values. In result my "repeater" prints only first element in collection.
<asp:Repeater ID="repeatAdministrators" OnItemDataBound="repeatAdministrators_ItemDataBound" runat="server">
<HeaderTemplate>
<tr>
<td class="formLabel">
Administrators:
</td>
<td class="formInputText">
</HeaderTemplate>
<ItemTemplate>
<asp:TextBox ID="txtAdministrators" runat="server" MaxLength="50" Enabled="False"></asp:TextBox><br />
</ItemTemplate>
<FooterTemplate>
</td> </tr>
<tr>
</FooterTemplate>
</asp:Repeater>
And here the code behind where I want to change read index in collection. AdminsEnumerator.MoveNext();
is not working as I expected. Adding something like int t; i++; StringCollection[i]
is annoying. Are there easier solutions?
protected void repeatAdministrators_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
TextBox txtAdministrators = e.Item.FindControl("txtAdministrators") as TextBox;
StringEnumerator AdminsEnumerator = Computer.Administrators.GetEnumerator();
AdminsEnumerator.MoveNext();
txtAdministrators.Text = AdminsEnumerator.Current;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的代码将无法工作,因为您将为数据绑定的每个项目获取一个新的枚举器并从头开始。 试试这个:
Repeater ItemTemplate:
代码(Page_Load):
Your code wouldn't work because you'll be getting a new enumerator for each item that is databound and starting over at the beginning. Try this instead:
Repeater ItemTemplate:
Code (Page_Load):