































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
It was developed by Microsoft to allow programmers to build dynamic web sites, applications and services. The name stands for Active Server Pages Network Enabled Technologies. ASP.NET (software) Developer(s) Microsoft.
Typology: Lecture notes
1 / 39
This page cannot be seen from the preview
Don't miss anything!
Unit II
Form Validation : Client Side Validation – Server Side Validation – Validation Controls :
Required Field – Comparison Range – Calendar Control – Ad Rotator Control – Internet
Explorer Control – Statement Management : View State – Session State – Control State –
Hidden Fields – Cookies – Query Strings – Application State – Session State.
Client Side Validation
In the Client Side Validation you can provide a better user experience by responding
quickly at the browser level. When you perform a Client Side Validation, all the user inputs
validated in the user's browser itself. Client Side validation does not require a round trip to the
server, so the network traffic which will help your server perform better. This type of validation
is done on the browser side using script languages such as JavaScript, VBScript or HTML attributes.
For example, if the user enter an invalid email format, you can show an error message
immediately before the user move to the next field, so the user can correct every field before they
submit the form.
ASP.NET client side coding has two aspects:
Client side scripts : It runs on the browser and in turn speeds up the execution of page. For example, client side data validation which can catch invalid data and warn the user accordingly without making a round trip to the server.
Client side source code : ASP.NET pages generate this. For example, the HTML source code of an ASP.NET page contains a number of hidden fields and automatically injected blocks of JavaScript code, which keeps information like view state or does other jobs to make the page work.
Client Side Source Code
We have already discussed that, ASP.NET pages are generally written in two files:
The content file or the markup file ( .aspx) The code-behind file The content file contains the HTML or ASP.NET control tags and literals to form the structure of the page. The code behind file contains the class definition. At run-time, the content file is parsed and transformed into a page class.
The code behind Button1_Click:
protected void Button1_Click(object sender, EventArgs e) {
if (!String.IsNullOrEmpty(TextBox1.Text)) {
// Access the HttpServerUtility methods through // the intrinsic Server object. Label1.Text = "Welcome, " + Server.HtmlEncode(TextBox1.Text) + ".
The url is " + Server.UrlEncode(Request.Url.ToString()) } }
ASP.NET validation controls validate the user input data to ensure that useless,
unauthenticated, or contradictory data don't get stored.
An important aspect of creating ASP.NET Web pages for user input is to be able to check that the information users enter is valid. ASP.NET provides a set of validation controls that provide an easy-to-use but powerful way to check for errors and, if necessary, display messages to the user.
There are six types of validation controls in ASP.NET
Required Field Validator
The RequiredFieldValidator is actually very simple, and yet very useful. You can use it to make sure that the user has entered something in a TextBox control. Let's give it a try, and add a RequiredFieldValidator to our page. We will also add a TextBox to validate, as well as a button to submit the form with.
RequiredFieldValidator Properties
Property Description
AccessKey It is used to set keyboard shortcut for the control.
BackColor It is used to set background color of the control.
BorderColor It is used to set border color of the control.
if(Page.IsValid) { btnSubmitForm.Text = "My form is valid!"; } }
CompareValidator
The CompareValidator might not be the most commonly used validator of the bunch, but it's still useful in some cases. It can compare two values, for instance the values of two controls.
CompareValidator Properties
Property Description
AccessKey It is used to set keyboard shortcut for the control.
TabIndex The tab order of the control.
BorderWidth It is used to set width of border of the control.
Font It is used to set font for the control text.
ForeColor It is used to set color of the control text.
Text It is used to set text to be shown for the control.
Visible To set visibility of control on the form.
Height It is used to set height of the control.
Width It is used to set width of the control.
ControlToCompare It takes ID of control to compare with.
ControlToValidate It takes ID of control to validate.
ErrorMessage It is used to display error message when validation failed.
Operator It is used set comparison operator.
In the next example, I will show you a small example of how it can be used.
Small number:
<asp:TextBox runat="server" id="txtSmallNumber" />
Big number:
<asp:TextBox runat="server" id="txtBigNumber" />
<asp:CompareValidator runat="server" id="cmpNumbers" controltovalidate="txtSmallNumber" controltocompare="txtBigNumber" operator="LessThan" type="Integer" errormessage="The first number should be smaller than the second number!" />
As you see, we only use one validator to validate the two fields. It might seem a bit overwhelming, but it's actually quite simple. Like with the RequiredFieldValidator, we use the controltovalidate attribute to specify which control to validate. In addition to that, we specify a control to compare. The operator attribute specifies which method to use when comparing. In this case, we use the LessThan operator, because we wish for the first control to have the smallest value. We set the type to integer, because we want to compare integers. Dates, strings and other value types can be compared as well.
Now, try running the website, and test the two new fields. Here is what happens if you don't fill out the form correctly:
Fig : Compare Validator
If you switch the numbers, you will see another cool thing about the ASP.NET validators and clientside scripting: Validation is also performed upon exiting the fields. If you don't want this, you can turn off clientside scripting with the enableclientscript attribute. You can also compare a field to a static value. To do this, remove the controltocompare attribute, and add the valuetocompare attribute.
However, as you may have noticed, the content of the two textboxes is not checked before comparing it. For instance, it would be possible to enter nothing, or enter a piece of text instead of a number. You should always consider this when using the CompareValidator. You can protect against empty fields with the RequiredFieldValidator, and you can protect against
Selecting a range of days Moving from month to month Controlling the display of the days programmatically
The Calendar control supports all the System.Globalization.Calendar types in the System.Globalization namespace. Apart from the Gregorian calendar, this also includes calendars that use different year and month systems, such as the Hijri calendar.
You can specify whether the Calendar control allows you to select a single day, week, or entire month by setting the SelectionMode property.
By default, the control displays the days of the month, day headings for the days of the week, a title with the month name and year, links for selecting individual days of the month, and links for moving to the next and previous month. You can customize the appearance of the Calendar control by setting the properties that control the style for different parts of the control. The following table lists the properties that specify the style for the different parts of the control.
The basic syntax of a calendar control is:
<asp:Calender ID = "Calendar1" runat = "server"> </asp:Calender>
Properties and Events of the Calendar Control
The calendar control has many properties and events, using which you can customize the actions and display of the control. The following table provides some important properties of the Calendar control:
Properties Description
Caption Gets or sets the caption for the calendar control.
CaptionAlign Gets or sets the alignment for the caption.
CellPadding Gets or sets the number of spaces between the data and the cell border.
CellSpacing Gets or sets the space between cells.
FirstDayOfWeek Gets or sets the day of week to display in the first column.
NextMonthText Gets or sets the text for next month navigation control. The default value is
.
NextPrevFormat Gets or sets the format of the next and previous month navigation control.
OtherMonthDayStyle Gets the style properties for the days on the Calendar control that are not in the displayed month.
PrevMonthText Gets or sets the text for previous month navigation control. The default value is <.
SelectedDate Gets or sets the selected date.
SelectedDates Gets a collection of DateTime objects representing the selected dates.
SelectedDayStyle Gets the style properties for the selected dates.
SelectionMode Gets or sets the selection mode that specifies whether the user can select a single day, a week or an entire month.
SelectMonthText Gets or sets the text for the month selection element in the selector
DayRender It is raised when each data cell of the calendar control is rendered.
VisibleMonthChanged It is raised when user changes a month.
Working with the Calendar Control
Putting a bare-bone calendar control without any code behind file provides a workable calendar to a site, which shows the months and days of the year. It also allows navigation to next and previous months.
Calendar controls allow the users to select a single day, a week, or an entire month. This is done by using the SelectionMode property. This property has the following values:
Properties Description
Day To select a single day.
DayWeek To select a single day or an entire week.
DayWeekMonth To select a single day, a week, or an entire month.
None Nothing can be selected.
The syntax for selecting days:
<asp:Calender ID = "Calendar1" runat = "server" SelectionMode="DayWeekMonth"> </asp:Calender>
When the selection mode is set to the value DayWeekMonth, an extra column with the > symbol appears for selecting the week, and a >> symbol appears to the left of the days name for selecting the month.
Example
The following example demonstrates selecting a date and displays the date in a label: The content file code is as follows:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="calendardemo._Default" %>
AdRotator is one of the Rich Web Controls available in ASP.NET. AdRotator control is available in ASP.Net to make the task of rotating the advertisement images in a web form quickly and easily.
It displays flashing banner advertisements randomly. It also enables you to display different types of images whenever you refresh the page. Adrotator control takes the information about the images that are to be displayed from an XML file which also have many other elements in it that are used by this control. The following lists some properties of Adrotator control:
_blank : displays the linked content in a new window without frames
_parent : displays the linked content in the parent window of the window that contains the link
_self : displays the linked content in the same window
_top : displays the linked content in the topmost window
Steps to get Adrotator to work
Create an advertisement file
The advertisement file is an XML file. The following are some of the elements of this XML file
Let us assume that the advertisement XML file is named as myads.xml. The file is as:
Create the application
State management is a preserve state control and object in an application because
ASP.NET web applications are stateless. A new instance of the Web page class is created each
time the page is posted to the server. If a user enters information into a web application, that
information would be lost in the round trip from the browser (MSDN). In a single line, State
management maintains and stores the information of any user till the end of the user session.
Hyper Text Transfer Protocol (HTTP) is a stateless protocol. When the client disconnects from the server, the ASP.NET engine discards the page objects. This way, each web application can scale up to serve numerous requests simultaneously without running out of server memory.
However, there needs to be some technique to store the information between requests and to retrieve it when required. This information i.e., the current value of all the controls and variables for the current user in the current session is called the State.
Control level: In ASP.NET, by default controls provide state management automatically.
Variable or object level: In ASP.NET, member variables at page level are stateless and thus
we need to maintain state explicitly.
Single or multiple page level: State management at single as well as multiple page level i.e.,
managing state between page requests.
User level: State should be preserved as long as a user is running the application.
Application level: State available for complete application irrespective of the user, i.e.,
should be available to all users.
Application to application level: State management between or among two or more
applications.
State management maintains and stores the information of any user till the end of the user
session.
STATE MANAGEMENT TECHNIQUES
Two types of State Management techniques are available in ASP.NET as in the following
figure,
Client-side | Techniques
Client-Side State Management techniques are,
View State Hidden field Cookies Control State Query Strings