1

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

Oedum
  • 796
  • 2
  • 9
  • 28
  • What do you have your ViewState set to.. is it false.. if so try making it ViewState = true; also what EventHandler are you checking this in..? – MethodMan Jan 07 '12 at 19:31
  • It seems like you should be trying to find the textbox with TextBox txt = item2.FindControl("TextBox1") as TextBox; -- not sure where you are getting 'MainContent_ParentRepeater_ChildRepeater_0_HB1_0' from? – TheGeekYouNeed Jan 08 '12 at 12:36

2 Answers2

1
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");
  }
}
MethodMan
  • 18,625
  • 6
  • 34
  • 52
  • I can't make it work... when i try to findcontrol the texztbox from codebehind it's still null – Oedum Jan 07 '12 at 23:25
0

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.

Chuck Savage
  • 11,775
  • 6
  • 49
  • 69