I have a GridView that looks something like this:
You must not bind your grid on postbacks in Page_Load, only when something changed that causes the Grid to reload data(f.e. Sorting,Paging) and only in the appropriate event-handlers.
Another possible reason: Have you disabled ViewState somewhere?
Did them all and got nowhere then discovered this: onrowcommand="GridView1_RowCommand" on the GridView1 definition line in html.
Tried the above answers and still could not get a post back. Ended up being a Unique ID issue. I had two <ItemTemplate>
with buttons that had the same ids. (In different grid views. My second one was in a User Control)
Changing the <asp:Button ID="" />
to a Unique ID solved the post back issue for me.
Just thought I'd post for any one else who tried the other options with no luck.
You can also check the HttpContext.Current.Request.Form["__EVENTTARGET"] and if it ends with the ID of the control, rebind the GridView and use Page.FindControl with the event target to find the control that fired the event
Put the grid.Databind()
inside if (!IsPostBack)
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
grid.DataBind();
}
}
I just had a colleague who encountered the same problem; his was caused by the onrowcommand=
attribute not being set in the asp:GridView
element. This should be set to the name of the handler which will be handling the event.
... just in case someone has the same issue!