Enabling Unobtrusive Validation Mode in ASP.NET 4.5

Introduction

 
In this article, we will learn how to Enable Unobtrusive Validation in ASP.NET 4.5.
 
Visual Studio 2012 provides some new Validation features that include Unobtrusive Validation. When you work with this Validation mode you will find that there is not much difference in this validation and previous validations but to enable this type of validation you had to first configure your Web Application.
 
There are three ways to enable the Unobtrusive Validation in your Web Application; they are:
  1. By using Web.Config file
  2. By using the Global.asax file
  3. By using the Page_Load event on each page
The first method is by using the Web.Config file.
 
Step 1
 
Write the following code in your Web.Config file:
  1. <configuration>  
  2.   <appSettings>  
  3.     <add key="ValidationSettings:UnobtrusiveValidationMode" value="None"></add>  
  4.   </appSettings>  
  5.     <system.web>  
  6.       <compilation debug="true" targetFramework="4.5" />  
  7.       <httpRuntime targetFramework="4.5" />  
  8.     </system.web>   
  9. </configuration> 
Step 2
 
Now write the following code in your WebForm.aspx Page:
  1. <asp:TextBox runat="server" ID="txt" />  
  2. <asp:RequiredFieldValidator ID="RequiredFieldValidator1" ErrorMessage="txt is required" ControlToValidate="txt" runat="server" Text="Text is Required" Display="Dynamic" />  
  3. <asp:Button ID="Button1" Text="Send info" runat="server" /> 
Step 3
 
Now debug your code, on debugging the code you will get the output like this:
 
valid1.jpg
 
When you click on the button in the output window you will see an Error Message. As you write in the Text Box and click again on the Button the error message will be disposed of automatically.
 
valid2.jpg
 
The second method is by using a Global.asax file.
 
Step 1
 
In the Global.asax file, first, add the namespace "using System.Web.UI;".
 
After adding the namespace write the following code in the Application_Start method:
  1. protected void Application_Start(object sender, EventArgs e)  
  2. {  
  3.     ValidationSettings.UnobtrusiveValidationMode = UnobtrusiveValidationMode.None;  
Step 2
 
Now write the following code in your WebForm.aspx page:
  1. <asp:TextBox runat="server" ID="txt" />  
  2. <asp:RequiredFieldValidator ID="RequiredFieldValidator1" ErrorMessage="txt is required" ControlToValidate="txt" runat="server" Text="Text is Required" Display="Dynamic" />  
  3. <asp:Button ID="Button1" Text="Send info" runat="server" /> 
Step 3
 
Again debugging your Web Application you will again get the same output as you got in the first method.
 
valid2.jpg
 
The third method is by simply writing the code on each page inside the Page_Load event.
 
Step 1
  1. protected void Page_Load(object sender, EventArgs e) {  
  2.     this.UnobtrusiveValidationMode = System.Web.UI.UnobtrusiveValidationMode.None;  
Step 2
 
Now write the following code in your WebForm.aspx page:
  1. <asp:TextBox runat="server" ID="txt" />  
  2. <asp:RequiredFieldValidator ID="RequiredFieldValidator1" ErrorMessage="txt is required" ControlToValidate="txt" runat="server" Text="Text is Required" Display="Dynamic" />  
  3. <asp:Button ID="Button1" Text="Send info" runat="server" /> 
Step 3
 
Again debug your Web Application and you will again get the same output as you got in the first method.
 
valid2.jpg


Recommended Free Ebook
Similar Articles