Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 597
Tyler Treat Avatar asked Dec 13 '11 19:12

Tyler Treat


3 Answers

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?

like image 163
Tim Schmelter Avatar answered Oct 18 '22 06:10

Tim Schmelter


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

like image 36
ShaileshDev Avatar answered Oct 18 '22 06:10

ShaileshDev


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!

like image 4
icecreamsoop Avatar answered Oct 18 '22 07:10

icecreamsoop