Click here to Skip to main content
15,891,864 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Hi all

I need some help here

I have this delete button in gridview

VB
<asp:CommandField ButtonType="Image" DeleteImageUrl="~/Images/remove.png"
                DeleteText="" ShowDeleteButton="True" />


how to add OnClick event handler to this button

and How to get the id from the row which the button exist



thank you so much
Posted
Comments
Sandeep Mewara 5-May-12 13:11pm    
What exactly are you trying to do? You trying to implement delete functionality? Any confirmation prompt before deleting? What exactly?
Sarah Mohammed77 5-May-12 14:26pm    
okay I have this button
<itemtemplate>
<asp:ImageButton ID="imgbtnDelete" runat="server" ImageUrl="~/Images/remove.png"
OnClick="AddToCart_Click" Text="delete"
CommandArgument='<%# Eval("ItemID") %>' Height="50px" Width="50px" postback="true" />


and the event handler for it like that

ImageButton myButton = sender as ImageButton;
if (myButton != null)
{
int ProductID = Convert.ToInt32(myButton.CommandArgument);
if (ProductID != 0)
{
for (int i = 0; i < Basket_DataTable.Rows.Count; i++)
if (Basket_DataTable.Rows[i][0].ToString() == ProductID.ToString())
Basket_DataTable.Rows.Remove(Basket_DataTable.Rows[i]);

}
}

when I click this button it gives me this error Invalid postback or callback argument

1 solution

See, you have entirely different issue and the way you asked/posted question was in different direction. You were trying to solve for something else.

Now, there could be few reasons why you are getting this issue. Easy/direct solution can be adding enableEventValidation="false" in your Page directive or Web.config file.

But, it is always good to hit the root cause. It might be that you are rebinding your grid on every postback. Thus, before RowCommand fire your GridView will rebind and all control within it will be assigned to new id. So RowCommand could not get the item which have fired the event. To avoid this, all you need to do is put your grid bind line in IsPostBack condition.
C#
if (!IsPostBack)
{
      //Your code for Bind data
}


Detailed explanation here: Invalid postback or callback argument solution. Event validation...[^]

Interesting to read one blog entry related to same: “Invalid postback or callback argument” in ASP.NET[^] - It exposes few other reasons on why one can get the same error. Nice read.
 
Share this answer
 
Comments
Sarah Mohammed77 5-May-12 15:48pm    
thank you so much it works
Sandeep Mewara 6-May-12 0:56am    
Good to know. :)
VJ Reddy 5-May-12 23:16pm    
Nice answer. 5!
Sandeep Mewara 6-May-12 0:56am    
Thanks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900