【问题标题】:Button inside a repeater with dropdownlist带有下拉列表的中继器内的按钮
【发布时间】:2010-05-27 12:02:38
【问题描述】:

我有一个带有文字、下拉列表和按钮的转发器。

  <asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="rep_ItemDataBound" onitemcommand="Repeater1_ItemCommand">
        <ItemTemplate>
          <div class="buypanel">
        <ul>
            <li>Choose finish <asp:DropDownList ID="ddlFinish" runat="server"></asp:DropDownList></li>
            <li>Qty <asp:Literal ID="ltQty" runat="server"></asp:Literal></li>
            <li><asp:Button ID="butBuy" runat="server" Text="Button" /></li>
        </ul>
        </div>
    </ItemTemplate>
    </asp:Repeater>

我在后面的代码中绑定了所有的信息,比如

 protected void rep_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Products product = (Products) e.Item.DataItem;


               //Dropdownlist to be bound. 

                //Set Buy Button
                var butBuy = (Button) e.Item.FindControl("butBuy");
                butBuy.CommandName = "Buy";
                butBuy.CommandArgument = product.Id.ToString();

            }
        }

我有我的 itemcommand 来获取按钮点击

  protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if(e.CommandName == "Buy")
            {

            }
        }

我不确定如何通过给定的按钮单击从文本框和旁边的下拉列表中获取正确的信息?

【问题讨论】:

    标签: .net webforms repeater itemcommand


    【解决方案1】:

    RepeaterCommandEventArgs 有一个“Item”属性,您可以使用它来引用发生按钮单击的特定项目(启动命令的项目)。然后,您可以使用相同的 FindControl 方法从控件中获取数据。

    根据您提供的示例代码,您可以使用 CommandArgument 属性来获取产品 ID。结合从控件收集的数据,您可以创建订单。

    【讨论】:

    • 感谢我的新事物,这将是一些简单的事情,但一如既往地知道那个简单的事情是什么......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-12
    • 2018-05-23
    • 2013-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多