2
<asp:Repeater ID="Cartridges" runat="server" onitemcommand="Cartridges_ItemCommand">
    <ItemTemplate>
        <p class="cartprice"><%#String.Format("{0:C}", Eval("Price"))%></p>
        <hr class="hr4" /> 
        <p class="cartqty">QTY <asp:TextBox ID="cartQty" Text="0" runat="server"></asp:TextBox> </p>
        <div class="cartbuy2"><asp:LinkButton ID="buy" runat="server" CommandName="AddtoCart" CommandArgument=<%#Eval("cartID") %> Text="Buy"></asp:LinkButton></div>
   </ItemTemplate>
</asp:Repeater>

如何在 CommandArgument 中传递文本框值?抱歉完全迷路了……

4

3 回答 3

3

你试过了吗:CommandArgument='<%#Eval("cartID") %>'

这与您的不同,因为它被单引号包围,我想这是正确的语法。

于 2011-06-20T15:12:09.877 回答
2

使用 FindControl 获取转发器 Item 中的其他项目。例如:

 protected void repeater_ItemCommand(object sender, RepeaterCommandEventArgs e)
 {
      LinkButton lb = (LinkButton)e.CommandSource;
      string textBoxValue = ((TextBox)lb.Parent.FindControl("cartQty")).Text;         
 }
于 2011-06-20T15:17:20.247 回答
0

您需要将 cartId 绑定到链接按钮 onItemDataBound 然后在 onItemCommand 上访问它,我已经为您修改了代码,试一试

    <asp:Repeater ID="Cartridges" runat="server" onitemcommand="Repeater_OnItemCommand" OnItemDataBound="Repeater_OnItemDataBound">
<ItemTemplate>
    <p class="cartprice"><%#String.Format("{0:C}", Eval("Price"))%></p>
    <hr class="hr4" /> 
    <p class="cartqty">QTY <asp:TextBox ID="cartQty" Text="0" runat="server"></asp:TextBox> </p>
    <div class="cartbuy2"><asp:LinkButton ID="buy" runat="server" CommandName="AddtoCart" Text="Buy"></asp:LinkButton></div>

你的 onItemdatabound 应该是这样的

 protected void Repeater_OnItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {

        //your code...

          LinkButton add = (LinkButton)e.Item.FindControl("buy");
                  add.CommandArgument = cartID.ToString();

    }

然后您可以像这样访问 item 命令上的文本框

 protected void Repeater_OnItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "AddtoCart")
            {
                LinkButton btnEdit = (LinkButton)e.CommandSource;
                if (btnEdit != null)
                {
                    string editId = btnEdit.CommandArgument;
                    string text = ((TextBox)e.Item.FindControl("cartQty")).Text;
                    //do some stuff with your cartid and quantity
                }
            }
}

您还可以通过添加更多链接按钮并将它们绑定到正确的命令然后在项目命令中访问它们来使用编辑/删除命令参数扩展您的代码谢谢

于 2011-06-20T16:13:41.113 回答