Use dropdownlist to implement errors in the three-level linkage of the new province and city without refreshing Ajax

Source: Internet
Author: User

Objectives:Use the dropdownlist control on the server side to implement three-level linkage between Ajax-free new provinces and cities

Save: <asp: dropdownlist id = "ddlprovince" runat = "server" enableviewstate = "false"> </ASP: dropdownlist>
City: <asp: dropdownlist id = "ddlcity" runat = "server" enableviewstate = "false"> </ASP: dropdownlist>
Area: <asp: dropdownlist id = "ddlcounty" runat = "server" enableviewstate = "false"> </ASP: dropdownlist>

 

Practice:

When loading a page, load the "Province" data first, add the "please select" item to the city and district respectively, and use the $. the post () method successfully achieves the smooth interaction between Ajax and other provinces and cities.

 

Problem:

However, when the page is submitted, the value of dropdownlist cannot be read in the background, and an error is prompted:

 

The callback parameter is invalid. Use <pages enableeventvalidation = "true"/> in the configuration, or use <% @ page enableeventvalidation = "true" %> On the page to enable event verification. For security purposes, this function verifies whether the parameters of the send-back or callback events come from the server controls that initially present these events. If the data is valid and is expected, use the clientscriptmanager. registerforeventvalidation method to register the sending back or callback data for verification.

 

I found this problem on the Internet and found that many people encountered this problem. Some people listed the following methods:

1,<% @ Page Language = "C #" autoeventwireup = "true" codefile = "default. aspx. add enableeventvalidation = "false" to CS "inherits =" _ default "%>.
2,Is form nesting. A page can only have one form. You can solve this problem by checking the code carefully.
3,If the page contains controls such as dropdownlist or ListBox, the following causes may occur:
3.1 Ajax is used in the drop-down menu, which is common in the provincial/municipal linkage menu. It may be because the initial item value of the drop-down menu is assigned to the ASPX page. This error is prompted when the event is returned, delete the initial item value from the drop-down menu and add the item to the binding event.
3.2 The reason is that the value attribute of the listitem in the dropdownlist control contains Chinese characters. You only need to change the value to English or numbers. You 'd better Add the following statement to Web. config:
<Globalization requestencoding = "UTF-8" responseencoding = "UTF-8" Culture = "ZH-CN" uiculture = "ZH-CN"/> because PostBack does not use UTF-8 encoding, javascript will think there is a problem.
Only change requestencoding = "UTF-8". responseencoding = "UTF-8" is not required.
4,Register for event Validation
The principle is to let Asp.net record this PostBack value.
Registerforeventvalidation must be called at render.

However, none of the above methods is available.

After enableeventvalidation = "false" is added to the first method, the error message is not displayed on the page, but the dropdownlist data cannot be read in the background.

 

Solution:

(Change dropdownlist to the client-side HTML control select and store it with Ajax. There will certainly be no errors, but here the form has a lot of data to be submitted, so do not consider using the client-side HTML Control)

1,<% @ Page Language = "C #" autoeventwireup = "true" codefile = "default. aspx. add enableeventvalidation = "false" in CS "inherits =" _ default "%> ";

2,Add the hidden field <input type = "hidden" id = "inputcity" runat = "server"/> on the page, and then assign the value of dropdownlist to the hidden field using Js,

In this way, the background can use inputcity. Value to read the value of the hidden domain, that is, the value selected by the province and city.

 

 

Related Article

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.