Validation Groups in Asp.NET 2.0
Posted On December 24, 2007 by Rose Mary filed under Internet
Inline Coding
Visual web Developer 2005 allows the inline coding. To build an ASP.NET inline page instead of using code behind model, simply select the page type from the add new Item dialog and uncheck the place Code in separate file check box. Unselect is necessary in the check box to build the inline coding. In Visual web Developer 2005 many pages will be having inline and code-behind styles.
Now we will add one button, two labels & text boxes to the webform in Design view.
After adding the controls click on the source tab. It will generate the following code automatically.
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<script runat="server">
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
Name:
<input id="Text1" type="text" />
<br />
<br />
Password:
<input id="Password1" type="password" /> <br />
<br />
<asp:Button ID="Button1" Runat="server" Text="Button" />
</div>
</form>
</body>
</html>
In This example you can notice the business logis is encapsulated in between <script> tab. One good future in inline model is that the business logic and presentation logic are contained in the same page.
Many developers like business logic and presentation logic code in the same page.
Code-behind Model
You can develop ASP.NET 2.0 pages using new code-behind model. To create a code-behind page from the visual web developer 2005 add new Item dialog. While adding a new Item Dialog you can observe a code-behind file option in the dialog. To build a code-behind page you have to select Place Code in a separate file option. The main reason t separate a code-behind model is to separate the business login and presentaion logins onto separate files. When you are developing legasy projects it will be very helpful.
Now we will add one button, two labels & text boxes to the webform in Design view. After adding the controls click on the source tab. It will generate the following code.
<%@ Page Language="VB" AutoEventWireup="false" CompileWith="Default2.aspx.vb" ClassName="Default2_aspx" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
Name:
<asp:TextBox ID="TextBox1" Runat="server"></asp:TextBox>
<br />
<br />
Password:
<input id="Password1" type="password" />
<br />
<br />
<asp:Button ID="Button1" Runat="server" Text="Button" />
</div>
</form>
</body>
</html>
The code-behind model has the attributes in the page directive different from the first code. The first attribute is CompileWith attribute in asp.net 2.0. This attribute is a new attribute in the page and is meant to the code-behind page that is used with this presentation page. In this case Default2.aspx.vb. The second attribute is ClassName attribute. This attribute specifies the name of the class that is bound to the page when the page is compiled.
Validation Groups in Asp.NET 2.0
The ASP.NET 2.0 framework included a rich set of validation controls that made it easy to validate form data before submitting the data to a server. The controls can be used against the form field validation, comparing the value of one form field against other and performaing custiom validation. One of the validations that provide you with a ValidationGroup property that enables you to separate the validation controls into a separate group.
Using Validation Groups
The Below code will illustrates how you can use validation groups with form controls. This page contains two TextBox controls. One Textbox control is associated with RequiredFieldValidator control and other one is associated with RangeValidator. Since the TextBox controls are in different validation groups, however, you can submit the TextBox controls independently.
Now we will explore how we can add two validation groups in a single page.
Add the following code in your source windows of visual web developer 2005.
A page with two validation groups.
<%@ Page Language="vb" %>
<script runat="server">
Sub Group1Click(ByVal s As Object, ByVal e As EventArgs)
If Page.IsValid Then
lblResult.Text = "Group 1 Submitted"
End If
End Sub
Sub Group2Click(ByVal s As Object, ByVal e As EventArgs)
If Page.IsValid Then
lblResult.Text = "Group 2 Submitted"
End If
End Sub
Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
End Sub
</script>
<html>
<head id="Head1" runat="server">
<title>Validation Groups</title>
</head>
<body>
<form id="Form1" runat="server">
<asp:Label ID="lblResult" Runat="Server" />
<fieldset style="padding:20px">
<legend>Frist Group 1</legend>Name :
<asp:TextBox
id="TextBox1"
Runat="Server" />
<asp:Button ID="Button1"
ValidationGroup="Group1"
Text="Submit"
OnClick="Group1Click"
Runat="Server" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1"
ValidationGroup="Group1"
ControlToValidate="TextBox1"
Text="(required)"
Runat="Server" Width="68px" Height="19px" />
</fieldset>
<fieldset style="padding:20px">
<legend>Second Group 2</legend>AGE:
<asp:TextBox id="TextBox3" Runat="Server" />
<asp:Button ID="Button2" ValidationGroup="Group2" Text="Submit" OnClick="Group2Click"
Runat="Server" />
<asp:RangeValidator id="ValiDateAge" ValidationGroup="Group2"
ControlToValidate="TextBox3"
MinimumValue="18"
MaximumValue="80"
Type="Integer"
EnableClientScript="false"
runat="server">
Sorry. Age should be between 18 and 80 only.
<P>
</asp:RangeValidator>
</fieldset>
</form>
</body>
</html>
In the above exmple, eatch of the buttons has a distinct validation group assinment. The first button on the form used the Group1 and the second button on the form uses the Group2. The each value validation control is associated with once of these validation groups. Becouse of this when the user clicks on the Group1 Submit button on the page ASP.Net recognizes that it should only with the vlidation server controls that have the same validation groupname that is Group1. ASP.Net ignores other validation controls. Once you add the code in your source window, execute the page. It will show you the result as shown in the Image2. You can modify the code according to your wish.
SetFocusOnError Property
There is other future that has been added to validation controls is a property called SetFocusOnError. When the form is submitted and user is not enter any value in the form, then it will throw error. This error will be in a Boolean value. The property places the page focus on the form element that receives the error.
SetFocusOnError Property is showen in the following example.
<asp:RequiredFieldValidator ID="RequiredFieldValidator1"
ValidationGroup="Group1"
ControlToValidate="TextBox1"
Text="(required)"
Runat="Server" Width="68px" Height="19px" setfocusonerror=true/>
Button Controls in ASP.NET 2.0
Button controls are basically used for submitting information and causing actions on a webpage. In ASP.NET 1.0/1.1 we used to integrate JavaScript code in the ASP.NET pages. When the button is clicked, JavaScript will be fired. The entire process is become complicated. Where as in ASP.NET 2.0 it is very simple. You can create a web page that has a JavaScript Event and server-side events when the button clicked.
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<script runat="server">
Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Response.Write("Client side Script")
End Sub
</script>
<script language= "javascript" >
function cscript()
{
alert ('Client side Script');
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" Runat="server" Text="Button" onClientClick="cscript()" OnClick="Button1_Click"/>
</div>
</form>
</body>
</html>
