Skip to main content

Unobtrusive validations in ASP.Net 4.5 Web Forms

With the release of ASP.Net 4.5 web forms there are tons of features added in the ASP.Net and Unobtrusive validations support is one of them. We have already seen that kind of validation in ASP.Net MVC and now we are going to have that in ASP.Net web forms. In this post I am going to explain how its works and how its different from earlier versions.

How validation was working with earlier versions?

In the earlier versions of ASP.Net it was working via putting a JavaScript for that. Let’s take a simple example. I have putted three things here. A textbox, required field validator and a button like following.

    <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
    <asp:Button ID="btnSubmit" runat="server" Text="Submit" />
    <asp:RequiredFieldValidator ID="reqname" runat="server"
         ErrorMessage="Name is required" ControlToValidate="txtName">
Now once you run browser and click on the button it validate the textbox and give a validation message like “Name is required”.

Unobtrusive validation in 4.5

Now, When you right click and view source. You will see that there is a JavaScript code embedded to page.

Unobtrusive validation in 4.5

And this is how the element will be rendered in html.

Unobtrusive validation in 4.5

So here everything is there based on JavaScript. It will create a page validator array and based on that it will validate when we submit form.

How Unobtrusive validations works in ASP.Net 4.5:

If you create web application in ASP.Net 4.5 It will enabled by default. So let’s see how its works. We are going to use same thing as above.

It’s works in same way as worked earlier when you click submit it will validate textbox and give a validation message “Name is required”.

Unobtrusive validation in 4.5

Now you done view source. It will look like following.

Unobtrusive validation in 4.5

Here you can see that It has added “data-val” attribute and based on that it will validate the control. It’s HTML5 way of doing validations. You can see it’s much cleaner then earlier version of

Prerequisites for Unobtrusive validations:

This validation requires a jQuery.js file as internally its using jQuery for the validations. So if you have created an empty ASP.Net 4.5 web application. You need to add following code in application_start event of global.asax file.
protected void Application_Start(object sender, EventArgs e)
        new ScriptResourceDefinition
            Path = "~/scripts/jquery-2.0.0.min.js",
            DebugPath = "~/scripts/jquery-2.0.0.js",
            CdnPath = "",
            CdnDebugPath = ""

How to configure Unobtrusive validations in ASP.Net 4.5:

There are few ways of configuring the Unobtrusive validations in ASP.Net 4.5. There is a property got added UnobtrusiveValidationMode  and It has two value.
  • None : It will tell that validation work in old fashion way. It will disable Unobtrusive validation.
  • WebForms: It will tell that it will have Unobtrusive validations.
There are multiple ways you can configure this property. You can put that app settings like following.
    <add key="ValidationSettings:UnobtrusiveValidationMode" value="None"/>
Or you can write at application_start event like below.
void Application_Start(object sender, EventArgs e)
    ValidationSettings.UnobtrusiveValidationMode = 
Or you can write in page_load event like following.
protected void Page_Load(object sender, EventArgs e)
    Page.UnobtrusiveValidationMode = UnobtrusiveValidationMode.WebForms;
That’s it. Hope you like it. Stay tuned for more..


  1. do you know how to make a function if obtrusive have validated by jquery ?

  2. Hi Following link might help you doing this.


Post a Comment

Your feedback is very important to me. Please provide your feedback via putting comments.

Popular posts from this blog

How to create Rest API(Web API) with ASP.NET Core 1.0

Recently Microsoft has released ASP.NET Core 1.0 RC2, I am getting lots of request from readers that about creating Web API. So I thought it will be a good idea to write a blog post about how to create Rest API(Web API) with ASP.NET Core 1.0.

So let’s get started via creating an ASP.NET Core 1.0 Web Application like following.

Once you click on ASP.NET Web Application, It will ask whether you need to create Web Application or Web API application. We are going to Web API so I am going to select Web API Application like following. Please note that in ASP.NET Core 1.0 there is no separate libraries or DLLs required for creating web APIs. This is just a project template.

Now once you click on OK It will create a Web API application with default values controller and program.cs.  As you know Program.cs is now starting point for the ASP.NET Core 1.0 application so It contains all the required configuration and startup items. Following is a code for that.
using System.IO; using Microsoft.Asp…

How to use migration with Entity Framework Core

Entity framework core is  the lightweight, extensible and cross-platform version of Entity Framework. Before some time, Microsoft has Released a new version of Entity Framework RC2. I have written a couple of blog post about Entity framework code first migration earlier for Entity framework 6.0. So there was a couple of request coming for writing a blog post about Entity Framework Core RC2 migration. So I thought it will be a good idea to give an overview how database migration works in Entity Framework Core RC2. This post will cover a basic scenario where we are going to create the database with existing ASP.NET Identity migration and then we are going to create a new model and have that migration applied in the database.

How to use Entity Framework Migrations: Let’s get started, To demonstrate entity framework core migrations, I am going to create a sample core web application like following.

Once we select core application it will appear the following dialog.

Now w…

Solution: gulp is not recognized as internal or external command- Visual Studio

Recently, After Upgrading to the newer version of Gulp, I was getting this error.  After doing lots of  finding online and in my system I was able to figure out that Modules like Gulp does not installed to the path. So when you try to run it from the command line it was showing below error.
gulp is not recognized as internal or external command How to solve : gulp is not recognized as internal or external command To solve this error we just need to create a new environment variable and set the NPM modules path there.

To create an environment variable,  Right click My Computer/ This Pc and click on properties.  It will open following windows.

Go to Advance System Settings and it will open system properties windows like below.

Click on Environment Variables it will open an environment and system variables windows like below.

Click on new System variable and put NODE_PATH in system variable name and %AppData%\npm\node_modules into the variable value like below.

Click on Ok. That’s it. N…