简体   繁体   中英

ASP.net event validation error using a button inside gridview

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. 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屏幕截图
I've been stuck on this for a while and setting the event validation = false seems too dangerous to me.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM