Ram Anugandula
Ram Anugandula

Reputation: 614

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

I have minimal knowledge of ASP.Net WebForms. Now I am facing a small issue. Here I am iterating list using foreach, while iterating I want to bind value linkbutton command argument. But OnCommand event I am not getting value.

enter image description here

MyCode:

.aspx

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

This is my event on .aspx.cs

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

I want pass value in command argument to my event. Please help

Upvotes: 0

Views: 934

Answers (1)

MrinmoyeeG
MrinmoyeeG

Reputation: 106

**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 ...
}

Upvotes: 1

Related Questions