在另一个转发器内找到转发器内的文本框

时间:2012-01-07 17:52:48

标签: c# asp.net repeater findcontrol

我制作了菜单清单。它由两个转发器组成,一个具有productType,另一个具有该产品类型的内容。 可以在文本框中输入您想要的内容数量,现在我想查找文本框及其内容。

这就是我的ASP.NET代码的样子:

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

这是我到目前为止所做的尝试:

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;
            }

首先进入parentrepeater并进入它的chilrepeaters。 但它无法找到我的文本框。

任何身体都有想法??

2 个答案:

答案 0 :(得分: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;
  }
}

您必须在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("MainContent_ParentRepeater_ChildRepeater_0_HB1_0");
  }
}

答案 1 :(得分:0)

尝试此类中的两种方法之一:(将此类放在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);
    }
}

您可以使用FindControlRecursive()查找特定的TextBox,并使用ModifyControl修改/对所有TextBox执行某些操作。