0% found this document useful (0 votes)
3 views

Visual Programming_Module 2

The document provides an overview of validation controls in ASP.NET, detailing various types such as RequiredFieldValidator, CompareValidator, and RangeValidator, along with their usage and benefits. It also explains state management techniques including ViewState, Session State, Application State, and the use of cookies for tracking user data. Additionally, it covers how to implement validation controls and manage user input effectively to enhance data quality and security in web applications.

Uploaded by

Leslie Qwer
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Visual Programming_Module 2

The document provides an overview of validation controls in ASP.NET, detailing various types such as RequiredFieldValidator, CompareValidator, and RangeValidator, along with their usage and benefits. It also explains state management techniques including ViewState, Session State, Application State, and the use of cookies for tracking user data. Additionally, it covers how to implement validation controls and manage user input effectively to enhance data quality and security in web applications.

Uploaded by

Leslie Qwer
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

CP1543: VISUAL PROGRAMMING S5 BCA

Module II: Introduction to validation controls, basic validation controls, validation techniques
and advanced validation controls. How to manage state – how to use view state, session state
and application state. How to use cookies.

Introduction to validation Controls


Validation controls
Validation controls in ASP.NET are used to validate the data that users enter into web
forms. This helps to ensure that the data is accurate and complete before it is processed
by the web application.

Types of validation controls


ASP.NET provides a number of different validation controls, each of which performs a
different type of validation. The most common validation controls are:

RequiredFieldValidator: Checks that a field is not empty.


CompareValidator: Compares the value of one field to the value of another field or to
a constant value.

RangeValidator: Checks that a field value is within a specified range.

RegularExpressionValidator: Checks that a field value matches a regular expression


pattern.

CustomValidator: Allows you to write your own custom validation code.

ValidationSummary: Displays a summary of all validation errors on the page.


Department of Computer Applications Page 1 of 20
CP1543: VISUAL PROGRAMMING S5 BCA

How to use validation controls?


To use a validation control, simply add it to your web form and associate it with the input
control that you want to validate. You can do this by setting the ControlToValidate
property of the validation control to the name of the input control.

Once you have added a validation control to your web form, you can configure it by
setting its properties. The most common properties to configure are:

ErrorMessage: The error message that is displayed when the validation control
detects an error.

Display: Specifies how the error message is displayed. The options are Dynamic,
Static and None.

Enabled: Specifies whether the validation control is enabled.

EnableClientScript: Specifies whether client-side validation is enabled for the


validation control.

ControlToValidate: Specifies the input control that the validator validates.

ErrorMessage: Specifies the error message that is displayed when the validator
detects an error.

Display: Specifies how the error message is displayed. The options are Dynamic,
Static, and None.

Enabled: Specifies whether the validator is enabled.

EnableClientScript: Specifies whether client-side validation is enabled for the


validator.

IsValid: Specifies whether the input data passed the validation test.

Benefits of using validation controls


There are a number of benefits to using validation controls in your ASP.NET web
applications, including:

Improved data quality: Validation controls help to ensure that the data that users
enter into your web forms is accurate and complete. This can help to reduce errors
in your web application and improve the overall user experience.

Reduced development time: Validation controls can save you time and effort when
developing web applications. You do not need to write your own code to validate
user input, as you can simply use the built-in validation controls.

Increased security: Validation controls can help to protect your web applications
from security attacks. For example, you can use validation controls to prevent users
from entering malicious code into your web forms.
Department of Computer Applications Page 2 of 20
CP1543: VISUAL PROGRAMMING S5 BCA

How ASP.NET processes validation controls


ASP.NET processes validation controls in the following steps:

1. When the user submits a web form, the ASP.NET runtime first validates the
input data using the validation controls that are associated with the form.

2. For each validation control, the ASP.NET runtime calls the Validate()method. The
Validate() Control performs the validation logic for the property of the control and sets
the Isvalid to indicate whether the validation was successful.

3. If any of the validation controls fail, the ASP.NET runtime aborts the form
submission and displays an error message to the user.

4. If all of the validation controls pass, the ASP.NET runtime processes the form
data as usual.
The ASP.NET runtime can perform validation on the client side or on the server side. Client-side
validation is performed using JavaScript code that is generated by the ASP.NET runtime. Server-side
validation is performed using code that is written in the C# or VB.NET programming language.

By default, validation is performed on the server side. However, you can also enable client-side
validation for individual validation controls by setting the EnableClientScript property of the
control to true.

Client-side validation is faster than server-side validation, but it is not as reliable. If the user
disables JavaScript in their web browser, client-side validation will not be performed.

Server-side validation is more reliable than client-side validation, but it is slower. Server-side
validation is also required for certain types of validation, such as validating that the user has
entered a valid credit card number.

You can also use a combination of client-side validation and server-side validation. For
example, you can enable client-side validation to perform basic validation on the user input and
then perform more complex validation on the server side.

Example
The following example shows how to use a validation control to validate a required field:

When the user submits the form, the ASP.NET runtime will validate the Name textbox using the
RequiredFieldValidator control. If the Name textbox is empty, the form will not be submitted and
the user will see the error message “Name is a required field”.

Department of Computer Applications Page 3 of 20


CP1543: VISUAL PROGRAMMING S5 BCA

Basic Controls (validation Controls)


Required field validator

The RequiredFieldValidator control makes a control required. If the user submits the
form without entering a value in the control, the validator will display an error message.

To use the RequiredFieldValidator control, you need to set the ControlToValidate


property to the ID of the control that you want to validate. You can also set the
ErrorMessage property to the message that you want to display if the validation fails.

Department of Computer Applications Page 4 of 20


CP1543: VISUAL PROGRAMMING S5 BCA

Required field validator that forces an option to be chosen from a list box

Department of Computer Applications Page 5 of 20


CP1543: VISUAL PROGRAMMING S5 BCA

Additional property of the required field validator

Property Description
The initial value of the control that's validated. If this value isn't
InitialValue
changed, the validation fails. The default is an empty string.

Compare validator
The CompareValidator control in ASP.NET allows you to compare the value of one
control to another control or to a specific value. You can use this control to validate
user input, such as ensuring that a password and password confirmation match, or
that a date is greater than another date.
To use the CompareValidator control, you need to set the following properties:

ControlToValidate: The ID of the control that you want to validate.

Operator: The type of comparison that you want to perform. The following
operators are available:

EqualTo

NotEqualTo

GreaterThan

LessThan

GreaterThanOrEqualTo

LessThanOrEqualTo

DataTypeCheck

ValueToCompare: The value to compare the control to. If you are comparing
the control to another control, you can set this property to the ID of the other
control.

Department of Computer Applications Page 6 of 20


CP1543: VISUAL PROGRAMMING S5 BCA

Department of Computer Applications Page 7 of 20


CP1543: VISUAL PROGRAMMING S5 BCA

Additional property of the compare validator

Property Description
The ID of the control that the value of the control specified
ControlToCompar
e in the ControlToValidate property should be compared to.

Range Validator
The RangeValidator control validates that the value of a control is within a specified
range. You can use this control to validate user input, such as ensuring that a quantity is
greater than zero or that a date is within a specific date range.

To use the RangeValidator control, you need to set the following properties:
ControlToValidate: The ID of the control that you want to validate.

MinimumValue: The minimum value that is allowed.

MaximumValue: The maximum value that is allowed.

Type: The data type of the values to compare. The following data types are
supported:

String

Integer

Double

Date

Currency

Department of Computer Applications Page 8 of 20


CP1543: VISUAL PROGRAMMING S5 BCA

Validation techniques
How to use Validation Summary Control
The Validation Summary control in ASP.NET Web Forms is a server-side control that
summarizes all the validation errors that occur on a page. It provides a convenient
way to display all the errors in a single location, making it easier for users to identify
and correct them.
Department of Computer Applications Page 9 of 20
CP1543: VISUAL PROGRAMMING S5 BCA

Validation summary can be used by:

1. Add the Validation Summary control to your page:


Drag and drop the Validation Summary control from the Toolbox onto your page,
or add the following code to your markup:

Department of Computer Applications Page 10 of 20


CP1543: VISUAL PROGRAMMING S5 BCA

This code creates two required field validators and a validation summary control on the web
page. The required field validators check whether the associated controls have a value. If the
controls do not have a value, the validators display an error message. The validation summary
control displays a summary of all the validation errors that have occurred on the page.

Department of Computer Applications Page 11 of 20


CP1543: VISUAL PROGRAMMING S5 BCA

How to use Validation groups


Validation groups in ASP.NET Web Forms allow you to organize validation controls

Department of Computer Applications Page 12 of 20


CP1543: VISUAL PROGRAMMING S5 BCA

on a page into sets. Each validation group can be validated independently of other
validation groups on the page. This is useful for cases where you want to validate
different parts of a form separately, or when you want to validate a form in stages.

To use validation groups, you first need to assign a validation group name to each
validation control on your page. You can do this by setting the
property of each validation control to the desired group name.

Once you have assigned validation groups to your validation controls, you can
validate each group independently by using the method and
passing in the name of the validation group.

Example

Advanced Validation Controls


Regular Expression Validator
Regular expression (regex) validation is a powerful tool for validating input based on a
specified pattern. Here are some examples of regular expressions for common

Department of Computer Applications Page 13 of 20


CP1543: VISUAL PROGRAMMING S5 BCA

validation scenarios in ASP.NET:

Department of Computer Applications Page 14 of 20


CP1543: VISUAL PROGRAMMING S5 BCA

How to manage State


How to use View state

Q. what is view state ?


The ViewState is a client-side state management technique that allows the server to
persist information between postbacks for a specific page. ViewState is used to store
the state of server-side objects and controls across multiple requests for the same page.

ViewState concept
1. The server generates a page with a textbox and stores the initial value of the
textbox in the ViewState.

2. The page is sent to the client, and the ViewState data is included as a hidden
field.

3. The user interacts with the page, for example by entering new text into the
textbox and clicking a button.

4. The updated value of the textbox and other control states are sent back to the
server during the postback.

5. The server uses the ViewState data to restore the previous state of the
controls, including the original value of the textbox.

6. Server-side code can now access the restored state of the controls and perform
any necessary processing based on user input.

Department of Computer Applications Page 15 of 20


CP1543: VISUAL PROGRAMMING S5 BCA

Department of Computer Applications Page 16 of 20


CP1543: VISUAL PROGRAMMING S5 BCA

Session state
Q What is session state?

Session state is a way to store data about a user's interaction with a website across
multiple page requests. This data can be used to track things like what items a user has
added to their shopping cart, or whether they are logged in.

Q How does session state work?

When a user visits a website, ASP.NET creates a unique session ID for the user. This
ID is stored in a cookie on the user's computer. When the user makes another request
to the website, the cookie is sent back to the server. ASP.NET then uses the session
ID to identify the user's session and access the session state data.

Q How to work with session state

To work with session state, you use the session object. This object provides
methods for storing and retrieving data from the session state.

Session State Operations:

Department of Computer Applications Page 17 of 20


CP1543: VISUAL PROGRAMMING S5 BCA

Application state
It is a global storage mechanism that is used to store data that needs to be available to
all users of an ASP.NET application. This data can be anything from configuration
settings to user state information. Unlike session state, which is specific to a
particular user's session, application state is shared among all users and is available
throughout the entire application.

Application state is stored in memory on the server, and it is available to all pages in an
ASP.NET application. This makes it ideal for storing data that needs to be accessed
frequently, such as configuration settings or user preferences.

Department of Computer Applications Page 18 of 20


CP1543: VISUAL PROGRAMMING S5 BCA

Example of Incrementing a Counter in Application State:


Incrementing a Counter in Application State with Locking:

Illustrates the importance of locking when updating a counter in application


state.

Department of Computer Applications Page 19 of 20


CP1543: VISUAL PROGRAMMING S5 BCA

Cookies
Cookies are small pieces of data stored on the user's device (usually a browser) by
websites. They are used to track user activity, store preferences, and maintain session
information.
Cookies, represented as name/value pairs, are pieces of data stored on the client's
computer. An example is the ASP.NET_SessionId cookie, which contains a typical
session ID generated by ASP.NET for session tracking.
Users can also create their own cookies by instantiating an object from the HttpCookie
class.

Cookies allow websites to remember user information and preferences, enhancing the
user experience and providing personalized content.

Department of Computer Applications Page 20 of 20

You might also like