Row command event not firing for image button in gridview?

4.6k Views Asked by At

I have a gridview with an image button in each row.I have set the command name and command argument .

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="1" CellSpacing="1" CssClass="one-column-emphasis" DataKeyNames="Id" OnRowCommand="GridView1_RowCommand" OnRowDataBound="GridView1_RowDataBound">

<asp:TemplateField>
                <ItemTemplate>
                    <asp:ImageButton ID="ImageButton1" runat="server" AlternateText="Register" CssClass="buttonreg" EnableTheming="False" CommandArgument='<%# Eval("Id") %>' CommandName="register" />
                </ItemTemplate>
                <ItemStyle HorizontalAlign="Center" />
            </asp:TemplateField>

I get the following error,

Invalid postback or callback argument. Event validation is enabled using in configuration or <%@ Page EnableEventValidation="true" %> in a page. For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them. If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.

What am I missing?

5

There are 5 best solutions below

0
On

The FillGrid() method must be called like.

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        FillGrid();
    }
}
2
On

When you are using gridview, bind this in

protected void Page_Load(Object sender, EventArgs e)
{
    if(!IsPostBack)
    {
        Binddatagrid();
    }
}

register your ClientScriptManager.RegisterForEventValidation Method postback method

0
On

Use this code

Html code:

<asp:GridView ID="mygrid" CssClass="reportgrid" EmptyDataRowStyle-CssClass="userwel"
                                        DataKeyNames="JudgementId, titles, Is_Active" AutoGenerateColumns="false" Width="800px" runat="server" OnRowCommand="mygrid_RowCommand">
<EmptyDataTemplate>  
 No Data Found.
</EmptyDataTemplate>
 <Columns>
<asp:TemplateField HeaderText="Name" HeaderStyle-HorizontalAlign="Left">
 <ItemTemplate>                                           
<asp:Label ID="label_Address" Text='<%# Bind("titles")%>' runat="server"></asp:Label>
 </ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Status" HeaderStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:Label ID="lblstatus" Text='<%# Bind("Is_Active")%>' runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Edit" ItemStyle-Width="43px">
<ItemTemplate>
<asp:ImageButton ID="imgedit" runat="server" alt="Edit" Height="40px" Width="40px"
src="../Images/Edit.png" CommandName="edit1" CommandArgument='<%# Container.DisplayIndex %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Delete" ItemStyle-Width="43px">
<ItemTemplate>
<asp:ImageButton ID="imgdelete" runat="server" alt="Edit" Height="40px" Width="40px"
src="../Images/delete.png" CommandName="delete1" CommandArgument='<%# Container.DisplayIndex %>' OnClientClick="return ConfirmationBox();" />
 </ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="View" ItemStyle-Width="43px">
<ItemTemplate>
<asp:ImageButton ID="imgview" runat="server" alt="Edit" Height="40px" Width="40px"
src="../Images/view.png" CommandName="view1" CommandArgument='<%# Container.DisplayIndex %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

.cs code

 protected void mygrid_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                int row = Convert.ToInt32(e.CommandArgument);        
                int JudgementId = Convert.ToInt32(gvL_Judgement.DataKeys[Convert.ToInt32(e.CommandArgument)]["JudgementId"]);
                if (e.CommandName == "edit1")
                {
                    //code to edit
                }
                if (e.CommandName == "view1")
                {
                    //code to view
                }
                if (e.CommandName == "delete1")
                {
                    //code to delete
                }
            }
            catch (Exception ex)
            {

            }
        }
0
On

Set AutoPostBack property of DropDownList to False.Make sure that in Page_Load event,

 protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
      //Call Grid() function.
    }

}

Call the Grid() function in the SelectIndexChanged event of DropDownList. Thankx all for help.

0
On

this is an old thread but thought of replying cause I had a hard time finding the solution for this.

The solution is you have to use if (!IsPostBack)

but in my case i had nothing written in my Page_Load event of the .aspx page but I had below code in my master page.

protected void Page_Load(object sender, EventArgs e)
{
    Page.DataBind();
}

once I put that Page.Databind(); inside if (!IsPostBack) problem solved.

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

Hope somebody find this usefull.