19

I have a GridView that looks something like this:

<asp:GridView 
    ID="GridView1"
    AllowPaging="true"
    OnRowCommand="RowCommand"
    OnPageIndexChanging="gridView_PageIndexChanging"
    Runat="server">
    <Columns>
        ...
        <asp:TemplateField>
            <ItemTemplate>
                <asp:Button ID="Button1" ButtonType="Button" CommandName="ItemExport" CommandArgument='<%# Eval("EXPORT") %>'
                    Text="Export" runat="server" />
            </ItemTemplate>
        </asp:TemplateField>
        ...
    </Columns>
 </asp:GridView>

Here is RowCommand:

protected void RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "ItemExport")
    {
        // etc.
    }
}

Clicking the button is not firing the RowCommand event at all. However, RowCommand fires when I click a page index in the GridView's pager.

2
  • 1
    When are you binding your grid to the DataSource, only if(!Postback)?! Dec 13, 2011 at 19:16
  • Yep, the problem was the binding was not wrapped in a if(!isPostBack) statement. Dec 13, 2011 at 19:49

10 Answers 10

53

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?

4
  • 7
    Can't believe this haunted me for hours...just sitting here staring blankly into some none functioning ASP.NET code...all because I had set EnableViewState="false" :@ Apr 9, 2013 at 16:05
  • 1
    When I bind GridView in if(!IsPostBack), pager does not work :( Sep 6, 2013 at 17:05
  • 1
    @MazdakShojaie You have to add an event handler for the GridView.PageIndexChanged event, and rebind in that handler. Check out this MSDN article msdn.microsoft.com/en-us/library/…
    – Zack
    Jan 9, 2014 at 15:50
  • 1
    For me it was the "EnableViewState" set to TRUE on the grdiview. Set that to FALSE & it started working. Thank you. Sep 21, 2015 at 13:50
6

Use CausesValidation="false" in button tag. It can solve the problem.

5

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!

0
3

Put the grid.Databind() inside if (!IsPostBack)

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        grid.DataBind();
    }
}
2

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.

0
2

If your code is like this:

protected void Page_Load(object sender, EventArgs e)
{
    BindGrid(dgv);
    if (!IsPostBack)
    {

    }
}

Put BindGrid() inside the !isPostBack block

1

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

1

My problem was requiredfieldvalidator

Solution to that was: I disable it at post back and then re-enable it after the row command.

0

Did them all and got nowhere then discovered this: onrowcommand="GridView1_RowCommand" on the GridView1 definition line in html.

0

I fixed my version of this problem by removing all event handlers from the GridView, then rebuilding the code, then adding them back in one at a time.

1
  • dont think this would change anything.
    – Homungus
    Jun 4, 2020 at 15:30

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.