2. Web Server controls
TextBox: A control used to capture user input.
Button: A control used to initiate an action on the server.
DropDownList: A control used to provide a list of options for the user to
choose from.
GridView: A control used to display and manipulate data in a tabular
format.
Calendar: A control used to display a calendar for selecting dates.
3. HTML Server Control
Textbox: A control used to capture user input.
Button: A control used to initiate an action on the server.
Dropdown list: A control used to provide a list of options for the user to
choose from.
Checkbox: A control used to capture boolean input.
Radio button: A control used to provide a list of mutually exclusive options
for the user to choose from.
Image: A control used to display images on the web page.
4. Web User Controls
Header/Footer controls: These controls are used to create consistent
headers and footers across different pages.
Navigation controls: These controls are used to create consistent
navigation menus across different pages.
Search controls: These controls are used to create consistent search
functionality across different pages.
Login controls: These controls are used to create consistent login
functionality across different pages.
5. Goals
Check validity of data from the end user
Why
To maintain data integrity
How
Check user input on the client side or on the server side
Examples
Is the name Textbox empty?
Does the email Textbox contain a structurally valid email
address?
6. Client Side Vs Server Side
Client-side validation
Done in the browser
Uses JavaScript
Does not require HTTP
request + response
Server-side validation
Done on the server
Uses C#
Requires HTTP request
+ response
7. RequiredFieldValidator
Input field cannot be empty
CompareValidator
Compare between user inputs using =, >, etc.
RangeValidator
Minimum < input < maximum
RegularExpressionValidator
Check the entry matches a pattern defined by the regular expression
CustomValidator
Make your own validator
ValidationSummary
Displays all error messages from validators in one spot
ASPNET Validation Controls
8. ControlToValidate
The control to be validated
ErrorMessage
The message used in the ValidationSummary
Text
The error message used in the validation control
CssClass
Style appearance of the messages
Common Properties of Validation Controls
9. The RequiredFieldValidator control ensures that the required field is not
empty. It is generally tied to a text box to force input into the text box.
The syntax for the control:
<asp:RequiredFieldValidator ID="rfvcandidate"
runat="server" ControlToValidate="ddlcandidate"
ErrorMessage="Please choose a candidate"
InitialValue="Please choose a candidate">
</asp:RequiredFieldValidator>
Required Field Validator
10. The CompareValidator control compares a value in one control with a fixed
value, or, a value in another control.
It has the following specific properties:
The basic syntax for the control:
<asp:CompareValidator ID="CompareValidator1" runat="server" ErrorMessage="CompareValidator">
</asp:CompareValidator>
Compare Validator
11. The RangeValidator control verifies that the input value falls within a
predetermined range.
It has three specific properties:
The syntax for the control:
<asp:RangeValidator ID="rvclass" runat="server" ControlToValidate="txtclass" ErrorMessage="Enter
your class (6 - 12)" MaximumValue="12" MinimumValue="6" Type="Integer">
</asp:RangeValidator>
Range Validator
12. The RegularExpressionValidator allows validating the input text by matching
against a pattern against a regular expression. The regular expression is set
in the ValidationExpression property.
The following table summarizes the commonly used syntax constructs for
regular expressions:
Regular Expression Validator
<asp:RegularExpressionValidator ID="RegExpVal2" runat="server"
ErrorMessage = "Please Enter Valid Email ID“ ValidationGroup="vgSubmit"
ControlToValidate="txtEmail" CssClass="requiredFieldValidateStyle"
ForeColor="Red" ValidationExpression="w+([-+.']w+)*@w+([-.]w+)*.w+([-.]w+)*">
</asp:RegularExpressionValidator>
13. The CustomValidator control allows writing application specific custom
validation routines for both the client side and the server side validation.
The server side validation routine should be written in any .Net language,
like C# or VB.Net.
The basic syntax for the control:
<asp:CustomValidator ID="CustomValidator1" runat="server"
ClientValidationFunction=.cvf_func.
ErrorMessage="CustomValidator"></asp:CustomValidator>
Custom Validator
14. The ValidationSummary control does not perform any validation but shows a
summary of all errors in the page. The summary displays the values of the
ErrorMessage property of all validation controls that failed validation.
Using Page.IsValid property we can check for page errors. When there are no
errors IsValid returns true and user can proceed to next page.
if (Page.IsValid)
{ //validation complete proceed }
runat="server"
The syntax for the control:
<asp:ValidationSummary ID="ValidationSummary1"
DisplayMode = "BulletList" ShowSummary = "true"
HeaderText="Errors:" />
Validation Summary
16. Validation Group
Second problem = when I click the Login button, only fields in the Login section
(Username & Password) needs to be validated. Along the same lines when I click the
"Register" button, only fields in the Registration section(Email, Username, Password and
ConfirmPassword) needs to validated. If we don't use validation groups, then by default,
whenever, you click any button, all the validation controls on the page get validated.
So, when you click the login button, and if you want only, the fields in the Login
section(Username and Password) to be validated, then set, the Validationgroup property
of the validation controls and the login button control to the same group name. Use a
different group name for the validation controls and register button, in the registration
section.
First Problem: when I click the Clear
button, Form validation still happens.
When I click the clear button, I just want
to clear the textboxes in the
Registration section. Validations doesn't
make any sense here. So, how do I
prevent validation from happening?
Answer: CausesValidation=“False”