我处理这个问题的方法是将你的 ArrayLists 处理成其他集合并绑定到那个集合。
例如,如果您的集合都具有相同数量的项目:
Dictionary<string, KeyValuePair<string, string>> combined =
new Dictionary<string, KeyValuePair<string, string>>();
for(int i = 0; i < list1.Count; i ++)
{
combined.Add(list1[i], new KeyValuePair(list2[i], list3[i]));
}
然后,在 itemdatabound 事件中处理嵌套转发器的绑定。这似乎是不必要的处理,但它会使逻辑更容易。
(要绑定您的 TextBox,您必须手动在行中找到控件并在 ItemDataBound 事件中设置 ID 和 Text)
编辑:这是我假设您正在尝试做的工作示例:
ASPX 代码:
<asp:Repeater ID="categoryRepeater" runat="server">
<HeaderTemplate><dl></HeaderTemplate>
<ItemTemplate>
<dt><em><%# Eval("Key") %></em></dt>
<dd>
<asp:Repeater ID="nestedRepeater" runat="server" DataSource='<%# Eval("Value") %>' >
<HeaderTemplate><ol></HeaderTemplate>
<ItemTemplate>
<li>
<asp:Label ID="lblSubCategory"
runat="server"
Text='<%# Eval("Key") %>' />
<asp:TextBox ID="txtInformation"
runat="server"
Text='<%# Eval("Value") %>' />
</li>
</ItemTemplate>
<FooterTemplate></ol></FooterTemplate>
</asp:Repeater>
</dd>
</ItemTemplate>
<FooterTemplate></dl></FooterTemplate>
</asp:Repeater>
代码隐藏(初始化数组和数据绑定):
ArrayList categories = new ArrayList
{ "Category", "Category 2",
"Category 3", "Category 4" };
ArrayList subCategories = new ArrayList
{ "SubCategory 1", "SubCategory 2",
"SubCategory 3", "SubCategory 4" };
ArrayList textControlNames = new ArrayList
{ "Enter 1", "Enter 2", "Enter 3", "Enter 4" };
Dictionary<string, Dictionary<string, string>> combined =
new Dictionary<string, Dictionary<string, string>>();
for (int i = 0; i < categories.Count; i++)
{
Dictionary<string, string> inner = new Dictionary<string, string>();
for (int j = 0; j < subCategories.Count;j++)
{
inner.Add(subCategories[j].ToString(),
textControlNames[j].ToString());
}
combined.Add(categories[i].ToString(), inner);
}
categoryRepeater.DataSource = combined;
categoryRepeater.DataBind();
请注意,由于您无法为标签或文本框生成相同的 ID,因此我没有像最初提到的那样包含 ItemDataBound 事件。