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