繁体   English   中英

读取转发器内的文本框

[英]read textbox inside a repeater

我有2个中继器可以打印菜单标题和菜单项 - 在另一个内部。 它们看起来像这样:

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

这一切都很好,很有趣,也很有效。 但现在我需要找到不同的文本框 - 在文本框中,您可以编写您想要的多少不同菜单项。 我尝试了很多不同的东西:

Control myControl1 = FindControl("MainContent_ParentRepeater_ChildRepeater_0_HB1_0");

和这个:

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,而不是呈现的客户端控件。 你应该能够做到这一点:

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.FindControl(“controlName”) - 而不是this.FindControl()。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM