Visual Programming_Module 2
Visual Programming_Module 2
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.
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.
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.
IsValid: Specifies whether the input data passed the validation test.
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
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”.
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.
Required field validator that forces an option to be chosen from a list box
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:
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.
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.
Type: The data type of the values to compare. The following data types are
supported:
String
Integer
Double
Date
Currency
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
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.
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
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.
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.
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.
To work with session state, you use the session object. This object provides
methods for storing and retrieving data from the session state.
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.
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.