GridView RowCommand event not firing

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.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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?

Method 2

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

Method 3

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!

Method 4

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

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

Method 5

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.

Method 6

If your code is like this:

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

    }
}

Put BindGrid() inside the !isPostBack block

Method 7

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

Method 8

My problem was requiredfieldvalidator

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

Method 9

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

Method 10

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.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x