Invalid postback or callback argument. Event validation is enabled using ''

后端 未结 30 1831
生来不讨喜
生来不讨喜 2020-11-22 05:08

I am getting the following error when I post back a page from the client-side. I have JavaScript code that modifies an asp:ListBox on the client side.

How do we fix

30条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-22 05:16

    I had an experience with DataGrid. One of it's columns was "Select" button. When I was clicking "Select" button of any row I had received this error message:

    "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."

    I changed several codes, and finally I succeeded. My experience route:

    1) I changed page attribute to EnableEventValidation="false". But it didn't work. (not only is this dangerous for security reason, my event handler wasn't called: void Grid_SelectedIndexChanged(object sender, EventArgs e)

    2) I implemented ClientScript.RegisterForEventValidation in Render method. But it didn't work.

    protected override void Render(HtmlTextWriter writer)
    {
        foreach (DataGridItem item in this.Grid.Items)
        {
            Page.ClientScript.RegisterForEventValidation(item.UniqueID);
            foreach (TableCell cell in (item as TableRow).Cells)
            {
                Page.ClientScript.RegisterForEventValidation(cell.UniqueID);
                foreach (System.Web.UI.Control control in cell.Controls)
                {
                    if (control is Button)
                        Page.ClientScript.RegisterForEventValidation(control.UniqueID);
                }
            }
        }
    }
    

    3) I changed my button type in grid column from PushButton to LinkButton. It worked! ("ButtonType="LinkButton"). I think if you can change your button to other controls like "LinkButton" in other cases, it would work properly.

提交回复
热议问题