Click here to Skip to main content
15,352,158 members
Articles / Web Development / HTML
Technical Blog
Posted 22 Nov 2011

Tagged as


8 bookmarked

Unobtrusive Validation with ASP.NET 4.5

Rate me:
Please Sign up or sign in to vote.
4.75/5 (12 votes)
22 Nov 2011CPOL2 min read
New and modified features of ASP.NET 4.5

ASP.NET 4.5 has been improved and is the latest version of ASP.NET. It includes an array of new and modified features. Today, we’ll discuss one new feature Unobtrusive Validation that is introduced with ASP.NET 4.5 .

So What is Unobtrusive Validation?

In a normal validation scenario, when we use a validator to validate any control and use Client side validation, some JavaScript is generated and rendered as HTML. Also some supportive JavaScript files also get loaded by the browser for the validation.

Now with feature Unobtrusive Validation, inline JavaScript is not generated and rendered to handle the Client Side validation. Instead of this, it uses HTML5 data-* attributes for all these validations.

So now, let's see a normal scenario. I have taken textbox and applied two ASP.NET validators on it. One is RequiredFieldValidator and RangeValidator that allows to enter the value between 100 and 1000. Let’s see the aspx code:

Image 1

Now let's run the application and see the generated the HTML. Now, if you look at the View Source of the page:

Image 2

To handle this validation, it generates lots of JavaScript code on the page. It is:
Image 3

Now I have made the same application with Visual Studio 2011 Developer Preview. By default, Unobtrusive Validation is enabled here. So let's see the generated HTML:

Click to enlarge image

Now if you see the above generated HTML, there are few data-* attributes that are rendered. And these hold all the required information of the validator. And there is no inline JavaScript code generated. This reduces the significant amount of page size because here inline JavaScript code is not generated and this also makes the rendered HTML neat and clean.

So let’s see how ASP.NET 4.5 allows to configure the Unobtrusive Validation. There is one new property UnobtrusiveValidationMode that got added, which can be assigned two values:

  • None: means UnobtrusiveValidation is set to off
  • WebForms: means UnobtrusiveValidation is enabled

This property can be configured at three places in the application. These are:

  • First, it can be set at application level in config file. We need to add it in the <appSettings> element.
    <add name="ValidationSettings:UnobtrusiveValidationMode" value="WebForms" />

    As I discussed, it is by default enabled in ASP.NET. The above key by default added in web.config. So if you want to disable it, then change the above as:

    <add name="ValidationSettings:UnobtrusiveValidationMode" value="None" />
  • We can also set it at Application_Start method in Global.asax file as:
    void Application_Start(object sender, EventArgs e)
       //Enabling UnobtrusiveValidation application wide
        ValidationSettings.UnobtrusiveValidationMode = 
  • We can also set it at page level by setting the property of Page class as:
    Page.UnobtrusiveValidationMode = System.Web.UI.UnobtrusiveValidationMode.WebForms;

I hope this post will help a lot to learn ASP.NET 4.5.


Image 5 Image 6 Image 7 Image 8 Image 9 Image 10 Image 11 Image 12


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer (Senior)
India India
Brij is a 3-times Microsoft MVP in ASP.NET/IIS Category and a passionate .NET developer. More than 6 years of experience in IT field, currently serving a MNC as a Tech Lead/Architect.

He is a very passionate .NET developer and have expertise over Web technologies like ASP.NET 2.0/3.5/4.0, jQuery, JSON, Javascript, IIS and related technologies. He is also a Exchange Server (EWS) Specialist. He has great experience in design patterns and N-Tier Architecture.

He is also certified as Microsoft Certified Technologies Specialist-ASP.NET and Microsoft Certified Technologies Specialist-WCF in .NET 4.0. He has also received several awards at various forums and his various articles got listed as "Article of the day" at ASP.NET Microsoft Official Website

He has done MCA from NIT Durgapur and completed his graduation from Lucknow University.

Learning new technologies and sharing knowledge excites him most. Blogging, solving problems at various forums, helping people, keeps him busy entire day.

Visit his Blog: Code Wala

Area of Expertise :
C#, ASP.NET 2.0,3.5,4.0, AJAX, JQuery, JSON, XML, XSLT, ADO.Net, WCF, Active Directory, Exchange Server 2007 (EWS), Java script, Web Services ,Win services, DotnetNuke, WSS 3.0,Sharepoint Designer, SQL Server 2000/2005/2008

Comments and Discussions

QuestionGreat Explanation Pin
Member 1225436818-Oct-17 16:06
MemberMember 1225436818-Oct-17 16:06 
GeneralMy vote of 2 Pin
OmarIsaid20-Sep-16 5:35
MemberOmarIsaid20-Sep-16 5:35 
GeneralMy vote of 3 Pin
Member 1108222426-May-16 20:10
MemberMember 1108222426-May-16 20:10 
QuestionVery nice explanation Pin
csugden17-Mar-14 10:58
professionalcsugden17-Mar-14 10:58 
QuestionGood Job Pin
mohanad19838-Jan-13 18:48
Membermohanad19838-Jan-13 18:48 
QuestionThanks! One typo found. Pin
Jonathon Book29-Nov-12 11:56
MemberJonathon Book29-Nov-12 11:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.