我正在为咖啡馆制作菜单。它显示了食物和溜冰场的物品,价格,然后我想要一个文本框,人们可以在其中写下他们想要购买的每一个,在文本框中显示。然后我希望每个文本框都有一个基于菜单项ID的唯一ID。
这是我的转发器:
<asp:Repeater ID="ParentRepeater" runat="server" OnItemDataBound="ParentRepeater_ItemDataBound">
<ItemTemplate>
<h2>
<%#DataBinder.Eval(Container.DataItem, "typenavn") %></h2>
<asp:HiddenField ID="HiddenField1" Value='<%# Eval("id") %>' runat="server" />
<asp:Repeater ID="ChildRepeater" runat="server">
<ItemTemplate>
<table>
<tr>
<td style="width: 400px">
<%#DataBinder.Eval(Container.DataItem, "productName") %>
</td>
<td style="width: 400px">
<%#DataBinder.Eval(Container.DataItem, "pris") %>
</td>
<td>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</td>
</tr>
</table>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
这是我背后的代码:
protected void ParentRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
RepeaterItem item = e.Item;
if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
{
Repeater ChildRepeater = (Repeater)item.FindControl("ChildRepeater");
HiddenField hide = e.Item.FindControl("HiddenField1") as HiddenField;
int id = Convert.ToInt32(hide.Value);
var query = from es in gr.products
where es.typeID == id
select es;
List<product> list = new List<product>();
foreach (product pro in query)
{
list.Add(pro);
}
ChildRepeater.DataSource = list;
ChildRepeater.DataBind();
int h = 0;
foreach (RepeaterItem item1 in ChildRepeater.Items)
{
if (item1.ItemType == ListItemType.Item || item1.ItemType == ListItemType.AlternatingItem)
{
TextBox txt = (TextBox)item1.FindControl("TextBox1") as TextBox;
HiddenField hf = (HiddenField)item1.FindControl("HiddenField2") as HiddenField;
for (int i = 0; i < list.Count; i++)
{
txt.ID = "HB" + list[h].id.ToString();
hf.Value = list[h].id.ToString();
h++;
break;
}
}
}
}
}
有人对如何找到文本框有任何想法吗?
答案 0 :(得分:1)
您必须在RepeaterItem中搜索TextBox。因此,您要么处理内部Repeater的ItemDataBound事件,要么只是迭代所有RepeaterItems:
foreach(RepeaterItem item in ChildRepeater.Items){
if(item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem){
var txt = (TextBox)item.FindControl("TextBox1");
}
}