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());
}
}
I've been stuck on this for a while and setting the event validation = false
seems too dangerous to me.