Analysis and resolution of various cases where ASP. NET postback or callback parameter is invalid

Source: Internet
Author: User

Yesterday, when implementing cascading menus, a sudden error occurred:

The postback or callback parameter is invalid. Use <pages enableeventvalidation= "true" in the configuration/> or use <%@ page enableeventvalidation= "true"%> to enable event validation. For security purposes, this feature verifies that the parameters of the postback or callback event originate from the server control that originally rendered the events. If the data is valid and is expected, use the Clientscriptmanager.registerforeventvalidation method to register the postback or callback data for validation.

Let's look at the hint to use <pages enableeventvalidation= "true" in the configuration/> or use <%@ page enableeventvalidation= "true"%> Event validation enabled We set the enableEventValidation property, set the value to true, that is, if event validation is enabled, is it possible to disable the event? Set the enableEventValidation property to False before running the program, you will find that the error is not, that is not the problem solved it? It may be that some people set false after the problem is resolved, maybe some people set false, the problem solved and caused a joint problem. What about event validation?

The instructions for MSDN are:

This event validation mechanism reduces the risk of unauthorized postback requests and callbacks. When the enableEventValidation property is set to True, ASP. NET allows only events that can be raised by the control during a postback request or callback.

Through this model, controls can register their events during rendering and then validate them during a postback or callback. By default, this feature is used by all event-driven controls in ASP.

Disabling event validation is highly recommended. If you do need to disable event validation, make sure that you do not construct a postback that has an unintended impact on your application.

In most cases, by setting the @ Page directive's enabledEventValidation property (Attribute) or the enableEventValidation property of the page element (Attribute) in the Web. config file To set the enableEventValidation property. If you set the enableEventValidation property in your code, it is set in the Page_Init stage of page processing.

There are several case scenarios for this type of error:

1.From Nesting Set workaround

First a page can have multiple form, but only one form is marked as runat= "server" and multiple form cannot be nested. Will the nested form cause this error?        I tried a few times without appearing the error in this article. However, if you submit the form when the form is not loaded, the error will appear in this article, but it is not related to the nested form nesting.

2.if (!this. Page.IsPostBack)

This error includes a number of situations and is subject to availability.

3. Cascading menus

Cause Analysis:

The Ajax app contains a drop-down list box (DropDownList), which is the case with the most frequent error, so why? Is that what it says on the internet? In fact, let us look at the AJAX application in the drop-down list box to do those things, the common is the city of AJAX applications, the drop-down list box in the market after the page load is no content, is based on the user's choice of the province asynchronous to the server request and then load the response into the City drop down list box, and then submit When the submission of the error of this article appears, that when the submission of what happened when the error occurred? When the page is rendered, the DropDownList render event iterates through the Value property of each DropDownList item, with the DropDownList's UniqueID attribute hash, and saves the result to the page. Saved in hidden with ID __eventvalidation, it might look like this <input type= "hidden" id= "__eventvalidation" name= "__eventvalidation" Value= "/wewbqkgg9abdqkd9shmbgkc9s ..."/> When we select the DropDownList of a province and submit it, the DropDownList event will validate the submitted content and ID is __eventvalidation value of hidden, because before the city's DropDownList there is no item, but when the submission we added several items and the event verification mechanism does not know, it will determine that the submitted data is not expected to be unauthorized, is invalid , I will report the error of this article. The solution is clear, as long as we tell the event validation mechanism that the data is valid and the problem will be solved.

Workaround:

You can replace the DropDownList control with a select when you do a cascading menu, such as:

Ui-->

<asp:dropdownlist id= "ddlcity" runat= "Server" >

</asp:DropDownList>

C#-->

string cityname = This.ddCity.SelectedItemValue;

To be replaced by:

Ui-->

<select id= "ddlcity" runat= "Server" >

</select>

C#->

String cityname = Request[this.ddlcity.uniqueid]

4. Is the ListItem Value property of the DropDownList control that contains the kanji. Just change the value to English or numeric.

Analysis and resolution of various cases where ASP. NET postback or callback parameter is invalid

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.