read textbox inside a repeater

2019-07-25 18:05发布

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??

2条回答
叛逆
2楼-- · 2019-07-25 18:28

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.

查看更多
我想做一个坏孩纸
3楼-- · 2019-07-25 18:40

The FindControl function should take the ID of the server control, not the rendered client control. You should be able to do this:

var txt = item.FindControl("TextBox1") as TextBox;
if (txt != null) 
{
    // found it!
}

To adjust your code:

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(("TextBox1")) as TextBox;
            }
        }
    }
}
查看更多
登录 后发表回答