Page 75 - DCAP312_WEB_TECHNOLOGIES_II
P. 75
Unit 4: Advanced Server Controls
There are a few things to be aware of when using the RequiredFieldValidator server control in Notes
our ASP.NET pages. First of all, the most important property of this validation control is the
ControlToValidate property. The value assigned to this control needs to be the value of the id
property for the control to which we want to link the RequiredFieldValidator control. We can
link only one RequiredFieldValidator server control to any other server control on the page.
For instance, if we have three required Textbox controls on our ASP.NET page, we need three
separate RequiredFieldValidator controls to make sure that they are all required.
The second property of this control is the Error Message property. This is the text that appears
where the RequiredFieldValidator control is located on the ASP.NET page if the Textbox is left
empty. Instead of using the Error Message property, we can also use the Text property:
<asp:RequiredFieldValidator id= “RequiredFieldValidator1”
runat= “server” Text= “Required!”
ControlToValidate= “TextBox1”>
</asp:RequiredFieldValidator>
Or we can use the following construct:
<asp:RequiredFieldValidator id= “RequiredFieldValidator1”
runat= “server” ControlToValidate= “TextBox1”>
Required!
</asp:RequiredFieldValidator>
The Compare Validator Control
The CompareValidator server control compares the value entered into the form pasture to one
more field, a database value, or other value that we identify. When comparing next to data types,
we just set the Operator DataTypeCheck. following that is done, we can set the Type attribute
to String, Integer, Double, Date, or Currency in the CompareValidator control to make sure that
the user’s input into the field is the specified type.
Using the CompareValidator server control, we can make comparisons between different controls
within a form on our ASP.NET page. For example, if we want to compare what the user enters in
the Password field to the entry in the Confirm Password field to see whether they are the same,
we can use the CompareValidator server control. Using these two fields is a common practice
when a form asks for a password. It ensures that the user does not mistype the password.
Using the Compare Validator Server Control
Visual C# .NET
<%@ Page Language= “C#” %>
<script runat= “server”>
Void Button1_Click(Object sender, EventArgs e) {
Label1.Text = “Passwords match”;
}
</script>
<html>
<head>
</head>
<body>
<form runat= “server”>
LOVELY PROFESSIONAL UNIVERSITY 69