Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 阅读中继器内的文本框_C#_Asp.net_Repeater - Fatal编程技术网

C# 阅读中继器内的文本框

C# 阅读中继器内的文本框,c#,asp.net,repeater,C#,Asp.net,Repeater,我有两个转发器,在另一个里面打印菜单标题和菜单项。 它们看起来像这样: <asp:Repeater ID="ParentRepeater" runat="server" OnItemDataBound="ParentRepeater_ItemDataBound"> <ItemTemplate> <h2> <%#DataBinder.Eval(Container.DataItem, "

我有两个转发器,在另一个里面打印菜单标题和菜单项。 它们看起来像这样:

<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: 200px">
                                <%#DataBinder.Eval(Container.DataItem, "productName") %>
                            </td>
                            <td style="width: 200px">
                                <%#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>
这是:

foreach (RepeaterItem item in ParentRepeater.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;
    }
}
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(("ct100$MainContent$ParentRepeater$ct100$ChildRepeater$ct100$HB1")) as TextBox; // MainContent_ParentRepeater_ChildRepeater_0_HB

             }
         }
     }
     break;
 }
这是:

foreach (RepeaterItem item in ParentRepeater.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;
    }
}
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(("ct100$MainContent$ParentRepeater$ct100$ChildRepeater$ct100$HB1")) as TextBox; // MainContent_ParentRepeater_ChildRepeater_0_HB

             }
         }
     }
     break;
 }
而且都不管用。外面有人能帮我吗??如何在中继器中获取文本框???

FindControl函数应获取服务器控件的ID,而不是呈现的客户端控件的ID。您应该能够做到这一点:

var txt = item.FindControl("TextBox1") as TextBox;
if (txt != null) 
{
    // found it!
}
要调整代码,请执行以下操作:

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(("TextBox1")) as TextBox;
            }
        }
    }
}
FindControl函数应采用服务器控件的ID,而不是呈现的客户端控件的ID。您应该能够做到这一点:

var txt = item.FindControl("TextBox1") as TextBox;
if (txt != null) 
{
    // found it!
}
要调整代码,请执行以下操作:

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(("TextBox1")) as TextBox;
            }
        }
    }
}

首先,您应该在要在其中查找控件的中继器上使用FindControl,例如,ParentRepeater.FindControlName,而不是this.FindControl


其次,您应该使用控件的ID,而不是客户端ID—这是一个不同的beast。

首先,您应该在要在其中查找控件的中继器上使用FindControl—例如,ParentRepeater.FindControl ControlName—而不是this.FindControl

其次,您应该使用控件的ID,而不是客户机ID——这是一个不同的beast