New Features of ASP. NET web forms 4.5 (2): HTML5 update and unobtrusive Validation

Source: Internet
Author: User

In the previous article, we introduced two new features: strong data control and bundling.

This time, we will introduce two new features: HTML5 update and unobtrusive validation in ASP. NET web forms 4.5.

HTML5 update

In ASP. NET web forms 4.5, the textboxmode of the control textbox has been increased from the previous three (singleline/multiline/password) to 16. For details, see msdn.

 

In this way, the downgrading of form-type pages will greatly reduce the verificationCodeTo improve development efficiency and put more human resources on the business logic.

The fileupload control finally supports Multifile upload and can be opened through the allowmultiple attribute.

For detailed usage, refer to the fileupload class description in msdn.

Of course, it also includes "~", for example, HTML5 Form Verification and HTML5 marking. Remove the root directory and add updatepanel support for HTML5 forms.

Unobtrusive Validation

The so-called unobtrusive validation is an implicit verification method that separates authentication code from HTML.

To learn more about the improvements, Let's first look at how the validation logic was handled before ASP. NET web forms 4.5.

We added this simple input form to the page.

1 < Body >
2 < Form ID = "Form1" Runat = "Server" >
3 < Div >
4 < Ul >
5 < Li >
6 < ASP: textbox ID = "Textbox_number" Runat = "Server" Textmode = "Singleline" > </ ASP: textbox >
7 < ASP: requiredfieldvalidator ID = "Requiredfieldvalidator1" Runat = "Server"
8 Errormessage = "* Required"
9 Controltovalidate = "Textbox_number"
10 Enableclientscript = "True" > </ ASP: requiredfieldvalidator >
11 < ASP: rangevalidator ID = "Rangevalidator1" Runat = "Server"
12 Controltovalidate = "Textbox_number"
13 Errormessage = "* Range 10 ~ 100"
14 Maximumvalue = "100"
15 Minimumvalue = "10" > </ ASP: rangevalidator >
16 </ Li >
17 < Li >
18 < ASP: button ID = "Button1" Runat = "Server" Text = "Submit"   /> </ Li >
19 </ Ul >
20 </ Div >
21 </ Form >
22 </ Body >

When we viewSource codeYou will find the following content.

The backend stores the verified JS Code on the page. When the page verification logic is complex, a large amount of Inline code is generated, and optimization (such as JS compression, CDN, page compression), maintenance is inconvenient.

In ASP. NET web forms 4.5, such verification logic can be separated.

You can add the following configuration in Web. config.

1 < Appsettings >
2 <! -- Enable unobtrusive validation, Which is enabled by default. -->
3 < Add Key = "Validationsettings: unobtrusivevalidationmode" Value = "Webform" />
4
5 <! -- Disable unobtrusive Validation -->
6 <! -- <Add key = "validationsettings: unobtrusivevalidationmode" value = "NONE"/> -->
7 </ Appsettings >

You can also add this segment to application_start.

1 Void Application_start ( Object Sender, eventargs E)
2 {
3 // Enabling unobtrusivevalidation application wide
4 Validationsettings. unobtrusivevalidationmode = unobtrusivevalidationmode. webforms;
5 }

If you only want to control a page to enable this function, you only need to add this code.

1 Page. unobtrusivevalidationmode = system. Web. UI. unobtrusivevalidationmode. webforms;

Now let's take a look at the HTML code generated to the client.

We found that the information of the verification class has changed to the attribute data-* that is unique to the form in HTML5 -*

The Inline JS and other content on the page have been processed and jquery is automatically introduced for verification. For details, refer to unobtrusive JavaScript.

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.