0

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

1 Answer 1

1
**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 ...
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.