Asp.net unit-II
Asp.net unit-II
Form Validation:
A web form has many input controls like TextBoxs. These controls should have right
type of data before submitting form at server side for data processing. Thus checking the
input controls called form validation.
Form validation includes-
1. Check control has required value.
2. Check value of control falls between minimum and maximum value.
3. Compare value of control against another value.
4. Check value of control is match with given format.
For example: If data is not input into TextBox then an error message should be display. It
is done using RequiredFieldValidator control.
1. Client side validation: When validation code is executed at client side by Browser
software called client side validation. Client side validation prevents form submission
to the server until all input values of control becomes error less. Validation code is
written / generated in JScript for client side validation. Client side validation saves
time for checking error that may consumes in between client to server and server to
client.
2. Server side validation: When validation code is executed at server side by any
programming language (C# / VB) called server side validation. Server side validation
happens after form submission to the server. At server side, values of controls are
checked. If any error founds then response to the client side using new web page.
This process consumes more time as compared to client side validation.
Validation Controls:
ASP.NET provides following set of validation controls that may validate value of form
control at client side (default) or server side.
1. RequiredFieldValidator
2. RangeValidator
3. CompareValidator
4. RegularExpressionValidator
5. CustomeValidator
N.MANIMOZHI-AP/ DEPARTMENT OF COMPUTER APPLICATION- RASC Page 1
22UCSCC62 : DOTNET PROGRAMMING
RequiredFieldValidator control:
This validation control checks to required value is input or not into specified input control
before submitting the form. This control needs to link any one input control like TextBox.
Syntax:
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" />
Important Properties:
- ControlToValidate: ID of control to be validate.
- Text: text to display for the validator when the validated control is invalid.
- ErrorMessage: Message to display in a ValidationSummary when the
validated control is invalid.
- ToolTip: The tooltip displayed when the mouse is over the control.
- EnableClientScript: If it is true then validation is performed at client side by
browser otherwise performed at server side.
CauseValidation property of Button: If false then button does not causes validation to
fire.
Example:
RangeValidator control:
This validation control checks input value falls between a certain minimum and
maximum value. This control needs to link any one input control like TextBox.
Syntax:
<asp:RangeValidator ID="RangeValidator1" runat="server" />
Important Properties:
- ControlToValidate: ID of control to be validate.
- MinimumValue: The minimum value for the control being validated.
- MaximumValue: The maximum value for the control being validated.
- Type: Data type of values for comparison. (String / Integer / Double / Date /
Currency)
- Text: text to display for the validator when the validated control is invalid.
- ErrorMessage: Message to display in a ValidationSummary when the
validated control is invalid.
- ToolTip: The tooltip displayed when the mouse is over the control.
- EnableClientScript: If it is true then validation is performed at client side by
browser otherwise performed at server side.
CauseValidation property of Button: If false then button does not causes validation to
fire.
Example:
CompareValidator control:
- Text: text to display for the validator when the validated control is invalid.
- ErrorMessage: Message to display in a ValidationSummary when the
validated control is invalid.
- ToolTip: The tooltip displayed when the mouse is over the control.
- EnableClientScript: If it is true then validation is performed at client side by
browser otherwise performed at server side.
CauseValidation property of Button: If false then button does not causes validation to fire.
Example1: Check data type.
Operator="Equal"
Text="(Password Not matched)" />
<br />
<asp:Button ID="Button1" runat="server" Text="Submit" />
<asp:Button ID="Button runat="server" Text="Cancel" CausesValidation="False" />
</div>
</form>
</body>
</html>
RegularExpressionValidator control:
This validation control compares input value against a regular expression. We can use a
regular expression to represent string pattern such as email address, dates etc.
Syntax:
<asp:RegularExpressionValidator ID="RegularExpressionValidator1"
runat="server" />
Important Properties:
- ControlToValidate: ID of control to be validate.
- ValidationExpression:The Regular expression is assigned to this property.
(http://regexlib.com for all list)
- Text: text to display for the validator when the validated control is invalid.
- ErrorMessage: Message to display in a ValidationSummary when the
validated control is invalid.
- ToolTip: The tooltip displayed when the mouse is over the control.
- EnableClientScript: If it is true then validation is performed at client side by
browser otherwise performed at server side.
CauseValidation property of Button: If false then button does not causes validation to
fire.
Example: Check valid EmailID format
ValidationSummary control:
This control display a list of all validation errors which are given in ErrorMessage
of each validation control.
Syntax:
<asp:ValidationSummary ID=" ValidationSummary1" runat="server" />
Important Properties:
- DisplayMode: Format for error summary display. (BulletList /
SingleParaGraph / List)
- HeaderText: To display header text on the top of summary control.
- ShowMessageBox: True means display a popup alert box.
- ShowSummary: False then summary hides.
CauseValidation property of Button: If false then button does not causes validation to
fire.
Example: Show summary of validation of all validation controls.
CustomValidator control:
If none of the other validation controls perform the type of validation that we need then
we can use CustomValidator control. In this control, we can associate function for a
custom validation.
Syntax:
<asp:CustomValidator ID="CustomValidator1" runat="server" />
Important Properties:
- ControlToValidate: ID of control to be validate.
- Text: text to display for the validator when the validated control is invalid.
- ErrorMessage: Message to display in a ValidationSummary when the
validated control is invalid.
- ServerValidate(Event): This event raised when the CustomValidator performs
validation.
CauseValidation property of Button: If false then button does not causes validation to
fire.
<html>
<head><title>Custome Validation Page</title></head>
<body>
<form id="form1" runat="server">
<div>
Input string of 10 character
<asp:TextBox ID="TextBox1" runat="server" />
<asp:CustomValidator ID="CustomValidator1" runat="server"
ControlToValidate="TextBox1"
OnServerValidate="CustomValidator1_ServerValidate" Text="(String Length is
greater than 10) />
<br />
<asp:Button ID="Button1" runat="server" Text="Submit" />
</div>
</form>
</body>
</html>
d (for digits)
w (for character)
{n} (n is exact number of allowed digits or character) [a-z](match any one
specified character)
+ (for one or more d/w)
\ (flow of d/w)
for example:
Calendar control:
The Calendar control displays a calendar that can use as a date picker or to display a list of
upcoming events.
Syntax:
<asp:Calendar ID="Calendar1" runat="server"/ >
Properties:
- Caption: The caption associated with calender.
- DayNameFormat: To set day name as(Mon/Monday/Mo/M.
- FirstDayOfWeek:Which day of week display first. (Sun/Mon/Tue..).
- NextMonthText: To set text for next month Button.(> for >).
- PrevMonthText: To set text for previous month Button. (< for <).
- NextPrevFormat: To set format for Next and Previous month
navigation buttons.(ShortMonth / FullMonth/ CustomText).
- SelectedDate: To get or set selected date.
- SelectedDates: To get multiple dates(C#).
- ShowDayHeader: If false then dayname becomes hides.
- ShowNextPrevMonth: If false then next and previous month hides.
- ShowTitle: If false then title of calender hides.
- TitleFormat: Set Month or Month Year.
- VisibleDate: Set the month of calender.
- SelectionMode: To set selection day/week/month.
Events:
- DayRender: Raised as each day is rendered.
- SelectionChanged: Raised when a new day, week, or month is selected.
- VisibleMonthChanged: Raised when the next or previous month link is clicked.
Example: Show all dates of selected week in a bullet list control.
<html>
<head><title>Get Selected Dates Page</title></head>
<body>
<form id="form1" runat="server">
<div>
<asp:Calendar ID="Calendar1" runat="server"
SelectionMode="DayWeekMonth"></asp:Calendar>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click"
Text="Submit" />
<asp:BulletedList ID="BulletedList1" runat="server"
DataTextFormatString="{0:d}"></asp:BulletedList>
</div>
</form>
</body>
</html>
AdRotator control:
<Advertisements>
<Ad>
<ImageUrl>~/AdImage/Ad1.jpg</ImageUrl>
<AlternateText>LRsir</AlternateText>
<NavigateUrl>http://www.LRsir.net</NavigateUrl>
<Impressions>50</Impressions>
<Keyword>banner</Keyword>
<Width>400</Width>
<Height>200</Height>
</Ad>
<Ad>
<ImageUrl>~/AdImage/Ad2.jpg</ImageUrl>
<AlternateText>Advance College</AlternateText>
<NavigateUrl>www.advcolcom</NavigateUrl>
<Impressions>20</Impressions>
<Keyword>banner</Keyword>
<Width>400</Width>
<Height>200</Height>
</Ad>
</Advertisements>
6) Attach adXML.xml file to AdvertisementFile property and add
Keyword value to KeywordFilter property.
<asp:AdRotator ID="AdRotator1" runat="server" KeywordFilter="banner"
AdvertisementFile="~/AdXMLFile.xml" />
When web page is refreshed then Ad images replaced by another Ad images. Number of
occurrence of any advertisement depends upon impressions value.
The MultiView control is responsible for displaying one View control at a time. The View
displayed is called the active view.
However, the View control cannot exist on its own. It would render error if you try to use
it stand-alone. It is always used with a Multiview control as:
Properties Description
A zero based index that denotes the active view. If no view is active, then the
ActiveViewIndex
index is -1.
The CommandName attribute of the button control associated with the navigation of the
MultiView control are associated with some related field of the MultiView control.
The following table shows the default command names of the above properties:
Properties Description
NextViewCommandName NextView
PreviousViewCommandName PrevView
SwitchViewByIDCommandName SwitchViewByID
SwitchViewByIndexCommandName SwitchViewByIndex
Methods Description
Every time a view is changed, the page is posted back to the server and a number of
events are raised. Some important events are:
Events Description
Apart from the above mentioned properties, methods and events, multiview control
inherits the members of the control and object class.
Example
The example page has three views. Each view has two button for navigating through the
views.
<head runat="server">
<title>
Untitled Page
</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2>MultiView and View Controls</h2>
<hr />
<br />
<asp:Button CommandArgument="0"
CommandName="SwitchViewByIndex" ID="btnfirst" runat="server" Text = "Go
To Next" />
<asp:Button CommandName="PrevView" ID="btnprevious"
runat="server" Text = "Go To Previous View" />
</asp:View>
</asp:MultiView>
</div>
</form>
</body>
</html>
The MultiView.ActiveViewIndex determines which view will be shown. This is the only
view rendered on the page. The default value for the ActiveViewIndex is -1, when no view
is shown. Since the ActiveViewIndex is defined as 2 in the example, it shows the third
view, when executed.