【问题标题】:Find a textbox inside repeater inside another repeater在另一个转发器内的转发器内查找文本框
【发布时间】:2012-01-07 17:52:47
【问题描述】:

我已经做了一个菜单列表。它由两个转发器组成,一个带有 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,然后进入它的 chilrepeater。 但它找不到我的文本框。

任何机构有和想法??

【问题讨论】:

  • 你的 ViewState 设置是什么。它是假的吗?如果是的话,试着让它 ViewState = true;还有什么 EventHandler 你在检查这个..?
  • 看来您应该尝试使用 TextBox txt = item2.FindControl("TextBox1") as TextBox; 查找文本框; -- 不确定您从哪里获得“MainContent_ParentRepeater_ChildRepeater_0_HB1_0”?

标签: c# asp.net repeater findcontrol


【解决方案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");
  }
}

【讨论】:

  • 我无法让它工作......当我尝试从代码隐藏中找到控制 texztbox 时,它仍然为空
【解决方案2】:

试试这个类的两种方法之一:(把这个类放到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 执行某些操作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-13
    • 1970-01-01
    相关资源
    最近更新 更多