在中继器中找不到我的文本框

发布于 2024-12-20 20:22:42 字数 3080 浏览 1 评论 0原文

我正在为一家咖啡馆制作菜单。它显示了食品和溜冰场的物品、价格,然后我想要一个文本框,人们可以在其中写下他们想要购买的每种物品的数量,显示在文本框中。然后我希望每个文本框都有一个基于菜单项 ID 的唯一 ID。

这是我的中继器:

<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:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                            </td>
                        </tr>
                    </table>
                </ItemTemplate>
            </asp:Repeater>
        </ItemTemplate>
    </asp:Repeater>

这是我的代码:

protected void ParentRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        RepeaterItem item = e.Item;

        if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
        {
            Repeater ChildRepeater = (Repeater)item.FindControl("ChildRepeater");

            HiddenField hide = e.Item.FindControl("HiddenField1") as HiddenField;
            int id = Convert.ToInt32(hide.Value);

            var query = from es in gr.products
                        where es.typeID == id
                        select es;

            List<product> list = new List<product>();
            foreach (product pro in query)
            {
                list.Add(pro);
            }

            ChildRepeater.DataSource = list;
            ChildRepeater.DataBind();

            int h = 0;

            foreach (RepeaterItem item1 in ChildRepeater.Items)
            {
                if (item1.ItemType == ListItemType.Item || item1.ItemType == ListItemType.AlternatingItem)
                {
                    TextBox txt = (TextBox)item1.FindControl("TextBox1") as TextBox;
                    HiddenField hf = (HiddenField)item1.FindControl("HiddenField2") as HiddenField;

                    for (int i = 0; i < list.Count; i++)
                    {
                        txt.ID = "HB" + list[h].id.ToString();
                        hf.Value = list[h].id.ToString();
                        h++;
                        break;
                    }
                }
            }
        }
    }

有人对如何找到文本框有任何想法吗?

I am making a menu for a cafe. It shows the food and rinks items, the price and then I want a textbox, where people can write how much of each they want to buy, shown in a textbox. Then I want each textbox to have a unique ID based on the menu items ID.

This is my repeater:

<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:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                            </td>
                        </tr>
                    </table>
                </ItemTemplate>
            </asp:Repeater>
        </ItemTemplate>
    </asp:Repeater>

And this is my code behind:

protected void ParentRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        RepeaterItem item = e.Item;

        if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
        {
            Repeater ChildRepeater = (Repeater)item.FindControl("ChildRepeater");

            HiddenField hide = e.Item.FindControl("HiddenField1") as HiddenField;
            int id = Convert.ToInt32(hide.Value);

            var query = from es in gr.products
                        where es.typeID == id
                        select es;

            List<product> list = new List<product>();
            foreach (product pro in query)
            {
                list.Add(pro);
            }

            ChildRepeater.DataSource = list;
            ChildRepeater.DataBind();

            int h = 0;

            foreach (RepeaterItem item1 in ChildRepeater.Items)
            {
                if (item1.ItemType == ListItemType.Item || item1.ItemType == ListItemType.AlternatingItem)
                {
                    TextBox txt = (TextBox)item1.FindControl("TextBox1") as TextBox;
                    HiddenField hf = (HiddenField)item1.FindControl("HiddenField2") as HiddenField;

                    for (int i = 0; i < list.Count; i++)
                    {
                        txt.ID = "HB" + list[h].id.ToString();
                        hf.Value = list[h].id.ToString();
                        h++;
                        break;
                    }
                }
            }
        }
    }

Anybody got any ideas about how to find the textbox??

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

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

发布评论

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

评论(1

您必须在 RepeaterItem< 中搜索文本框/a>.因此,您要么处理内部 Repeater 的 ItemDataBound 事件,要么简单地迭代所有 RepeaterItem:

foreach(RepeaterItem item in ChildRepeater.Items){
  if(item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem){
    var txt = (TextBox)item.FindControl("TextBox1");
  }
}

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