在另一个中继器内的中继器内查找文本框

发布于 2024-12-25 14:00:41 字数 2472 浏览 1 评论 0原文

我已经列了一份菜单清单。它由两个转发器组成,一个包含产品类型,另一个包含该产品类型的内容。 可以在文本框中输入您想要的内容的数量,我现在想要找到该文本框及其内容。

这就是我的 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;
            }

首先进入父中继器,然后进入它的子中继器。 但它找不到我的文本框。

任何机构有和想法吗?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

大海や 2025-01-01 14:00:41
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 事件,要么简单地迭代所有 RepeaterItem:

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");
  }
}
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");
  }
}
蓝咒 2025-01-01 14:00:41

尝试此类中的两种方法之一:(将此类放入 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 进行修改/执行某些操作。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文