如何使用命令参数 Asp.Net WebForms C# 将值传递给 Click 事件

How to pass value to Click event using command argument Asp.Net WebForms C#

我对 ASP.Net WebForms 知之甚少。现在我面临一个小问题。这里我使用 foreach 迭代列表,同时迭代我想绑定值 linkbutton 命令参数。但是 OnCommand 事件我没有得到价值。

我的代码:

.aspx

<%foreach (var item in model.ItemsWithoutDiscount) {%>
      <asp:Button ID="Button1" runat="server" Text="Delete" CommandArgument='<%= item.ID %>' OnCommand="Button1_Click" />
<%}%>

这是我在 .aspx.cs

上的活动
protected void Button1_Click(Object sender, CommandEventArgs e)
{
        string ID = e.CommandArgument.ToString();
}

我想将命令参数中的值传递给我的事件。请帮忙

**ASPX**

<asp:Repeater ID="Repeater1" runat="server" OnItemCommand="Repeater1_ItemCommand">
    <ItemTemplate>
      
        <asp:Button runat="server" CommandArgument='<%# Eval("ID") %>' Text="Delete" />
    </ItemTemplate>
</asp:Repeater>

**C#**
protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
       Repeater1.DataSource=YOUR_DATA_SOURCE;//ItemsWithoutDiscount
        Repeater1.DataBind();

        // ...
    }
}

protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
    if (e.CommandArgument == null) 
    return;
    var id = int.Parse(e.CommandArgument.ToString());
    
    // your logic here ...
}