Top Banner
Server control, validation control Session 10
31
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: ASP.NET Session 10

Server control, validation control

Session 10

Page 2: ASP.NET Session 10

Objectives• Calendar Server Control• Panel server control • Overview of validation controls • Client-Side versus Server-Side Validation• RequiredField Validator• Compare Validator• Range Validator• RegularExpression Validator• Custom Validator• Validation Summary

Page 3: ASP.NET Session 10

Calendar Server Control

The Calendar server control is a rich control that enables you to place a full-featured calendar directly on your Web pages. It allows for a high degree of customization to ensure that it looks and behaves in a unique manner. The Calendar control, in its simplest form, is coded in the

following manner:<asp:Calendar ID="Calendar1" runat="server"></asp:Calendar>

Page 4: ASP.NET Session 10

Panel server control The Panel server control encapsulates a set of controls you can

use to manipulate or lay out your ASP.NET pages. It is basically a wrapper for other controls, enabling you to take a group of server controls along with other elements (such as HTML and images) and turn them into a single unit.

The advantage of using the Panel control to encapsulate a set of other elements is that you can manipulate these elements as a single unit using one attribute set in the Panel control itself.

<asp:Panel ID="Panel1" runat="server" Height="300" Width="300“ ScrollBars="auto">

Page 5: ASP.NET Session 10

Overview of validation controls

People have been constructing Web applications for a number of years. Usually the motivation is to provide or gather information. If you collect data with your applications, collecting valid data should be important to you. If the information isn ’ t valid, there really isn ’ t much point in collecting it.

Page 6: ASP.NET Session 10

Overview of validation controls

• Validation is a set of rules that you apply to the data you collect.

• The trick is to find the right balance of the fewest rules and the proper strictness, without compromising the usability of the application.

Page 7: ASP.NET Session 10

Client-Side versus Server-Side Validation

server-side validation If you are new to Web application development, you might

not be aware of the difference between clientside and server-side validation. Suppose that the end user clicks the Submit button on a form after filling out some information. What happens in ASP.NET is that this form is packaged in a request and sent to the server where the application resides. At this point in the request/response cycle, you can run validation checks on the information submitted. Doing this is called server-side validation because it occurs on the server.

Page 8: ASP.NET Session 10

Client-Side versus Server-Side Validation

client-side validation On the other hand, supplying a script (usually in the

form of JavaScript) in the page that is posted to the end user’s browser to perform validations on the data entered in the form before the form is posted back to the originating server is also possible. In this case, client-side validation has occurred.

Page 9: ASP.NET Session 10

Required Field Validator

The RequiredFieldValidator control simply checks to see whether something was entered into the HTML form element. It is a simple validation control, but it is one of the most frequently used. You must have a RequiredFieldValidator control for each form element on which you want to enforce a value-required rule.

Page 10: ASP.NET Session 10

RequiredFieldValidator <form id="form1" runat="server"><div> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1"

ErrorMessage="Required"></asp:RequiredFieldValidator><br /><asp:Button ID="Button1" runat="server" Text="Submit"OnClick="Button1_Click" /><br /><br /><asp:Label ID="Label1" runat="server"></asp:Label></div></form>

Page 11: ASP.NET Session 10

The CompareValidator Server Control

The CompareValidator control enables you to compare two form elements as well as to compare values contained within form elements to constants that you specify. For example, you can specify that a form element’s value must be an integer and greater than a specified number. You can also state that values must be strings, dates, or other data types that are at your disposal.

Page 12: ASP.NET Session 10

<form runat="server" id="Form1"><p>Password<br /><asp:TextBox ID="TextBox1" runat="server"TextMode="Password"></asp:TextBox>&nbsp;<asp:CompareValidator ID="CompareValidator1"runat="server" Text="Passwords do not match!"ControlToValidate="TextBox2"ControlToCompare="TextBox1"></asp:CompareValidator></p><p>Confirm Password<br /><asp:TextBox ID="TextBox2" runat="server"TextMode="Password"></asp:TextBox></p><p>

Page 13: ASP.NET Session 10

<asp:Button ID="Button1" OnClick="Button1_Click"

runat="server" Text="Login"></asp:Button></p><p><asp:Label ID="Label1"

runat="server"></asp:Label></p></form>

Page 14: ASP.NET Session 10

The RangeValidator Server Control

The RangeValidator control is quite similar to that of the CompareValidator control, but it makes sure that the end-user value or selection provided is within a specified range as opposed to being just greater than or less than a specified constant. For an example of this control, think back to the text-box element that asks for the age of the end user and performs a validation on the value provided.

Page 15: ASP.NET Session 10

<form runat="server" id="Form1"><p>Age:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>&nbsp;<asp:RangeValidator ID="RangeValidator1" runat="server"ControlToValidate="TextBox1" Type="Integer“Text="You must be between 30 and 40"MaximumValue="40" MinimumValue="30"></asp:RangeValidator></p><p><asp:Button ID="Button1" OnClick="Button1_Click"runat="server" Text="Submit"></asp:Button></p><p><asp:Label ID="Label1" runat="server"></asp:Label></p></form>

Page 16: ASP.NET Session 10
Page 17: ASP.NET Session 10

The RegularExpressionValidator

Server Control One exciting control that developers like to

use is the RegularExpressionValidator control. This control offers a lot of flexibility when you apply validation rules to your Web forms. Using the RegularExpressionValidator control, you can check a user’s input based on a pattern that you define using a regular expression.

Page 18: ASP.NET Session 10

Making sure the text-box value is an e-mail address

<form runat="server" id="Form1"><p>Email:<asp:TextBox ID="TextBox1"

runat="server"></asp:TextBox>&nbsp; <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="You must type correct Email Address" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"

Page 19: ASP.NET Session 10

ControlToValidate="TextBox1"></asp:RegularExpressionValidator>

</p>

<p><asp:Button ID="Button1" OnClick="Button1_Click"runat="server" Text="Submit"></asp:Button></p><p><asp:Label ID="Label1" runat="server"></asp:Label></p></form>

Page 20: ASP.NET Session 10

CustomValidator Server Control

So far, you have seen a wide variety of validation controls that are at your disposal. In many cases, these validation controls address many of the validation rules that you want to apply to your Web forms. Sometimes, however, none of these controls works for you, and you have to go beyond what they offer. This is where the CustomValidator control comes into play.

Page 21: ASP.NET Session 10

CustomValidator Server Control

CustomValidator Server Control is of two type:

1. Using Client-Side Validation

2. Using Server-Side Validation

Page 22: ASP.NET Session 10

Client-Side Validation

One of the worthwhile functions of the CustomValidator control is its capability to easily provide custom client-side validations. Many developers have their own collections of JavaScript functions they employ in their applications, and using the CustomValidator control is one easy way of getting these functions implemented.

Page 23: ASP.NET Session 10

<script type="text/javascript">function validateNumber(oSrc, args) {args.IsValid = (args.Value % 5 == 0);}</script> <form id="form1" runat="server"><div><p>Number:<asp:TextBox ID="TextBox1"runat="server"></asp:TextBox>&nbsp;<asp:CustomValidator ID="CustomValidator1"runat="server" ControlToValidate="TextBox1"Text="Number must be divisible by 5"ClientValidationFunction="validateNumber">

Page 24: ASP.NET Session 10

</asp:CustomValidator></p><p><asp:Button ID="Button1" OnClick="Button1_Click"runat="server" Text="Button"></asp:Button></p><p><asp:Label ID="Label1" runat="server"></asp:Label></p></div></form>

Page 25: ASP.NET Session 10

Server-Side Validation

Now let’s move this same validation check from the client to the server. The CustomValidator control enables you to make custom server-side validations a reality as well. You will find that creating your server-side validations is just as easy as creating client-side validations.

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>&nbsp;<asp:CustomValidator ID="CustomValidator1" runat="server"

ControlToValidate="TextBox1" Text="Number must be divisible by 5"

OnServerValidate="ValidateNumber"></asp:CustomValidator>

Page 26: ASP.NET Session 10

ValidationSummary Control

The ValidationSummary server control works with all the validation server controls on the page. It takes all the error messages that the other validation controls send back to the page and puts them all in one spot (that you specify) on the page. These error messages can be displayed in a list, bulleted list, or paragraph.

Page 27: ASP.NET Session 10

Example<form id="form1" runat="server"><p>First name <asp:TextBox id="TextBox1"

runat="server"></asp:TextBox> &nbsp; <asp:RequiredFieldValidator id="RequiredFieldValidator1" runat="server" ErrorMessage="You must enter your first

name"

ControlToValidate="TextBox1"></asp:RequiredFieldValidator>

</p><p>Last name

Page 28: ASP.NET Session 10

<asp:TextBox id="TextBox2" runat="server"></asp:TextBox>

&nbsp; <asp:RequiredFieldValidator id="RequiredFieldValidator2" runat="server" ErrorMessage="You must enter your last name" ControlToValidate="TextBox2"></asp:RequiredFieldValidator></p>

<p> <asp:Button id="Button1" onclick="Button1_Click" runat="server" Text="Submit"></asp:Button>

Page 29: ASP.NET Session 10

</p><p> <asp:ValidationSummary id="ValidationSummary1"

runat="server" HeaderText="You received the following errors:"> </asp:ValidationSummary></p><p> <asp:Label id="Label1" runat="server"></asp:Label></p></form>

Page 30: ASP.NET Session 10

Output

Page 31: ASP.NET Session 10

Summary

• Requirement of validation in web page

• Different types of validation controls in asp.net

• Applications of validation control

• Combination of validation controls