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

后端 未结 30 1363
生来不讨喜
生来不讨喜 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: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);
    }
    

提交回复
热议问题