0

I have a page that that has multiple buttons. When a single button is clicked, everything is fine but after it does the postback and another button is clicked it generates the exception: System.Web.HttpUnhandledException (0x80004005): Exception of type 'System.Web.HttpUnhandledException' was thrown. ---> System.ArgumentException: Invalid postback or callback argument. Event validation is enabled using <pages enableEventValidation="true"/> in configuration or <%@ Page EnableEventValidation="true" %> in a page.

Here's the aspx code:

    <asp:GridView ID="gvSafeList" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" AllowPaging="True" AllowSorting="True" OnRowCommand="gvSafeList_RowCommand" PageSize="15">
    <Columns>
        <asp:BoundField DataField="EmailTo" HeaderText="Account" SortExpression="EmailTo" />
        <asp:BoundField DataField="EmailFromName" HeaderText="From" SortExpression="EmailFromName" />
        <asp:BoundField DataField="EmailFrom" HeaderText="Senders Email" SortExpression="EmailFrom" />
        <asp:BoundField DataField="Subject" HeaderText="Subject" SortExpression="Subject" />
        <asp:BoundField DataField="Time_Stamp" HeaderText="Time" SortExpression="Time_Stamp" />
        <asp:TemplateField>
          <ItemTemplate>
            <asp:Button ID="AddButton" runat="server" 
              CommandName="AddToSafeList" 
              CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"
              Text="Unblock & Restore" />
          </ItemTemplate> 
        </asp:TemplateField>
    </Columns>
    <PagerSettings FirstPageText="First" LastPageText="Last" Mode="NumericFirstLast" NextPageText="Next" PageButtonCount="20" Position="TopAndBottom" PreviousPageText="Previous" />
    <PagerStyle Font-Bold="True" Font-Size="15pt" HorizontalAlign="Center" />
</asp:GridView>

Here's my c# code:

    protected void gvSafeList_RowCommand(object sender, GridViewCommandEventArgs e)
{
    try
    {
       //Do some database stuff...
        if (e.CommandName == "AddToSafeList")
        {
//More stuff
            Response.Redirect("~/Spam",false);

            lblResults.Text = "Successfully updated. Emails from this recipient will be delivered to your inbox within the next 5 minutes.";
        }
    }
    catch (Exception ex)
    {
        LogIt(ex.ToString());

    }

}

Gridview Screenshot
I've been stuck on this for a while and setting the event validation = false seems too dangerous to me.

AbdulRahman Ansari
  • 3,007
  • 1
  • 21
  • 29
DigitalRayne
  • 389
  • 2
  • 3
  • 14

1 Answers1

2

Do you have codes in you Page_Load events? if yes, then perhaps by adding the following will help.

if (!Page.IsPostBack)
{ 
    //do something
}

This error is thrown when you click on your command and the Page_load is being ran again, in a normal life cycle will be Page_Load -> Click on Command -> Page_Load (again) -> Process RowCommand Event

Vikas Rana
  • 1,961
  • 2
  • 32
  • 49
  • Thanks :) I'm going to wait 24 hours before I close this but I was unable to generate the error. It may have fixed it :) – DigitalRayne Jul 15 '14 at 05:04