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

后端 未结 30 1416
生来不讨喜
生来不讨喜 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:11

    If you fill the DropdownList through client side script then clear the list before submit the form back to server; then ASP.NET will not complain and the security will be still on.

    And to get the data selected from the DDL, you can attach an "OnChange" event to the DDL to collect the value in a hidden Input or in a textbox with Style="display: none;"

    0 讨论(0)
  • 2020-11-22 05:11

    Ajax UpdatePanel makes it, and I think it's the easiest way, ignoring the Ajax postback overhead.

    0 讨论(0)
  • 2020-11-22 05:13

    I had the same problem when modifying a ListBox using JavaScript on the client. It occurs when you add new items to the ListBox from the client that were not there when the page was rendered.

    The fix that I found is to inform the event validation system of all the possible valid items that can be added from the client. You do this by overriding Page.Render and calling Page.ClientScript.RegisterForEventValidation for each value that your JavaScript could add to the list box:

    protected override void Render(HtmlTextWriter writer)
    {
        foreach (string val in allPossibleListBoxValues)
        {
            Page.ClientScript.RegisterForEventValidation(myListBox.UniqueID, val);
        }
        base.Render(writer);
    }
    

    This can be kind of a pain if you have a large number of potentially valid values for the list box. In my case I was moving items between two ListBoxes - one that that has all the possible values and another that is initially empty but gets filled in with a subset of the values from the first one in JavaScript when the user clicks a button. In this case you just need to iterate through the items in the first ListBoxand register each one with the second list box:

    protected override void Render(HtmlTextWriter writer)
    {
        foreach (ListItem i in listBoxAll.Items)
        {
            Page.ClientScript.RegisterForEventValidation(listBoxSelected.UniqueID, i.Value);
        }
        base.Render(writer);
    }
    
    0 讨论(0)
  • 2020-11-22 05:14

    What worked for me is moving the following code from page_load to page_prerender:

    lstMain.DataBind();
    Image img = (Image)lstMain.Items[0].FindControl("imgMain");
    
    // Define the name and type of the client scripts on the page.
    String csname1 = "PopupScript";
    Type cstype = this.GetType();
    
    // Get a ClientScriptManager reference from the Page class.
    ClientScriptManager cs = Page.ClientScript;
    
    // Check to see if the startup script is already registered.
    if (!cs.IsStartupScriptRegistered(cstype, csname1))
    {
        cs.RegisterStartupScript(cstype, csname1, "<script language=javascript> p=\"" + img.ClientID + "\"</script>");
    }
    
    0 讨论(0)
  • 2020-11-22 05:15

    This was the reason why I was getting it:

    I had an ASP:ListBox. Initially it was hidden. At client side I would populate it via AJAX with options. The user chose one option. Then when clicking the Submit button, the Server would get all funny about the ListBox, since it did not remember it having any options.

    So what I did is to make sure I clear all the list options before submitting the form back to the server. That way the server did not complain since the list went to the client empty and it came back empty.

    Sorted!!!

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题