I have 2 repeaters that print menu headers and menu items - on inside the other. They look like this:
<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: 200px">
<%#DataBinder.Eval(Container.DataItem, "productName") %>
</td>
<td style="width: 200px">
<%#DataBinder.Eval(Container.DataItem, "pris") %>
</td>
<td>
<asp:HiddenField ID="HiddenField2" runat="server" />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
</td>
</tr>
</table>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>
It's all good and fun and works. But now I need to find the different textboxes - in the textboxes you can write how many of the different menu items you want. I have tried many different things:
Control myControl1 = FindControl("MainContent_ParentRepeater_ChildRepeater_0_HB1_0");
And this:
foreach (RepeaterItem item in ParentRepeater.Items)
{
if (item.ItemType == ListItemType.Item)
{
TextBox txt = (TextBox)item.FindControl(("MainContent_ParentRepeater_ChildRepeater_0_HB1_0")) as TextBox;
// do something with "myTextBox.Text"
break;
}
}
And this:
foreach (RepeaterItem item1 in ParentRepeater.Items)
{
if (item1.ItemType == ListItemType.Item || item1.ItemType == ListItemType.AlternatingItem)
{
ChildRepeater = (Repeater)item1.FindControl("ChildRepeater");
foreach (RepeaterItem item2 in ChildRepeater.Items)
{
if (item2.ItemType == ListItemType.Item || item2.ItemType == ListItemType.AlternatingItem)
{
TextBox txt = (TextBox)item2.FindControl(("ct100$MainContent$ParentRepeater$ct100$ChildRepeater$ct100$HB1")) as TextBox; // MainContent_ParentRepeater_ChildRepeater_0_HB
}
}
}
break;
}
And none of it work. Can anybody out there help me?? How do I get hold of my textbox inside the repeater??
First, you should use FindControl on the repeater you wish to locate a control within - for instance, ParentRepeater.FindControl("controlName") - as opposed to this.FindControl().
Secondly, you should use the ID of the control, not the Client ID - which is a different beast.
The
FindControl
function should take the ID of the server control, not the rendered client control. You should be able to do this:To adjust your code: