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

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

    I've had the same problem, what I did:

    Just added a condition if(!IsPostBack) and it works fine :)

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

    If you know up front the data that could be populated, you can use the ClientScriptManager to resolve this issue. I had this issue when dynamically populating a drop down box using javascript on a previous user selection.

    Here is some example code for overriding the render method (in VB and C#) and declaring a potential value for the dropdownlist ddCar.

    In VB:

    Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
    
        Dim ClientScript As ClientScriptManager = Page.ClientScript
    
        ClientScript.RegisterForEventValidation("ddCar", "Mercedes")
    
        MyBase.Render(writer)
    End Sub
    

    or a slight variation in C# could be:

    protected override void Render(HtmlTextWriter writer)
    {
        Page.ClientScript.RegisterForEventValidation("ddCar", "Mercedes");
        base.Render(writer);
    }
    

    For newbies: This should go in the code behind file (.vb or .cs) or if used in the aspx file you can wrap in <script> tags.

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

    I know that this is a super-old post. Assuming that you are calling into your application, here is an idea that has worked for me:

    1. Implement the ICallbackEventHandler on your page
    2. Call ClientScriptManager.GetCallbackEventReference to call your server side code
    3. As the error message states, you could then call ClientScriptManager.RegisterForEventValidation

    If you don't need total control, you could use an update panel which would do this for you.

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

    If you are using Ajax update panel. Add <Triggers> tag and inside it trigger the Button or control causing the postBack using <asp:PostBackTrigger .../>

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

    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 ItemCommand Event

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

    I was using datalist and I was getting the same error for my push button. I just use IsPostBack to check and fill my controls and the problem is solved! Great!!!

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