Oedum
Oedum

Reputation: 816

Find a textbox inside repeater inside another repeater

I have made a menu list. It consists of two repeater, one with the productType and the other with the content of that product type. It is possible to enter how many of the content you want in a text box and I now want to find the textbox and its content.

This is how my ASP.NET code looks like:

<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:HiddenField ID="HiddenField2" runat="server" />
                                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
                            </td>
                        </tr>
                    </table>
                </ItemTemplate>
            </asp:Repeater>
        </ItemTemplate>
    </asp:Repeater>

This is what I have tried to do so far:

Repeater ChildRepeater;

            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(("MainContent_ParentRepeater_ChildRepeater_0_HB1_0")) as TextBox; // MainContent_ParentRepeater_ChildRepeater_0_HB

                        }
                    }
                }
                break;
            }

First going into the parentrepeater and the going into it's chilrepeaters. But it cant find my textbox.

Any body have and idea??

Upvotes: 1

Views: 2565

Answers (2)

Chuck Savage
Chuck Savage

Reputation: 11945

Try one of the two methods in this class: (Put this class in App_Code)

using System.Web;
using System;
using System.Web.UI;
using System.Web.UI.WebControls;


/// <summary>
/// Summary description for ControlHelper
/// </summary>
public static class ControlHelper
{
    // Example: HtmlForm form = ControlHelper.FindControlRecursive(this.Master, "form1") as HtmlForm;
    /// <summary>
    /// Finds a Control recursively. Note finds the first match and exits
    /// </summary>
    /// <param name="ContainerCtl"></param>
    /// <param name="IdToFind"></param>
    /// <returns></returns>
    public static Control FindControlRecursive(this Control Root, string Id)
    {
        if (Root.ID == Id)
            return Root;

        foreach (Control Ctl in Root.Controls)
        {
            Control FoundCtl = FindControlRecursive(Ctl, Id);
            if (FoundCtl != null)
                return FoundCtl;
        }

        return null;
    }

    //ModifyControl<TextBox>(this, tb => tb.Text = "test");
    public static void ModifyControl<T>(this Control root, Action<T> action) where T : Control
    {
        if (root is T)
            action((T)root);
        foreach (Control control in root.Controls)
            ModifyControl<T>(control, action);
    }
}

You'd use FindControlRecursive() to find a specific TextBox and you'd use ModifyControl to modify/do something with all the TextBox's.

Upvotes: 0

MethodMan
MethodMan

Reputation: 18843

foreach ( RepeaterItem item1 in Repeater.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;
  }
}

or

You have to search for the TextBox in the RepeaterItem. So you either handle the inner Repeater's ItemDataBound event or you simply iterate all RepeaterItems:

foreach(RepeaterItem item in ChildRepeater.Items){
  if(item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem){
    var txt = (TextBox)item.FindControl("MainContent_ParentRepeater_ChildRepeater_0_HB1_0");
  }
}

Upvotes: 1

Related Questions