- •Preface
- •Who Should Read This Book
- •Organization and Presentation
- •Contacting the Authors
- •Acknowledgments
- •Contents
- •Introduction
- •Why Microsoft .NET?
- •The Microsoft .NET Architecture
- •Internet Standards
- •The Evolution of ASP
- •The Benefits of ASP.NET
- •What Is .NET?
- •.NET Experiences
- •.NET Clients
- •.NET Services
- •.NET Servers
- •Review
- •Quiz Yourself
- •Installation Requirements
- •Installing ASP.NET and ADO.NET
- •Installing the .NET Framework SDK
- •Testing Your Installation
- •Support for .NET
- •Review
- •Quiz Yourself
- •Designing a Database
- •Normalization of Data
- •Security Considerations
- •Review
- •Quiz Yourself
- •Creating a Database
- •Creating SQL Server Tables
- •Creating a View
- •Creating a Stored Procedure
- •Creating a Trigger
- •Review
- •Quiz Yourself
- •INSERT Statements
- •DELETE Statements
- •UPDATE Statements
- •SELECT Statements
- •Review
- •Quiz Yourself
- •The XML Design Specs
- •The Structure of XML Documents
- •XML Syntax
- •XML and the .NET Framework
- •Review
- •Quiz Yourself
- •ASP.NET Events
- •Page Directives
- •Namespaces
- •Choosing a Language
- •Review
- •Quiz Yourself
- •Introducing HTML Controls
- •Using HTML controls
- •How HTML controls work
- •Intrinsic HTML controls
- •HTML Control Events
- •The Page_OnLoad event
- •Custom event handlers
- •Review
- •Quiz Yourself
- •Intrinsic Controls
- •Using intrinsic controls
- •Handling intrinsic Web control events
- •List Controls
- •Rich Controls
- •Review
- •Quiz Yourself
- •Creating a User Control
- •Adding User Control Properties
- •Writing Custom Control Methods
- •Implementing User Control Events
- •Review
- •Quiz Yourself
- •Common Aspects of Validation Controls
- •Display property
- •Type Property
- •Operator Property
- •Using Validation Controls
- •RequiredFieldValidator
- •RegularExpressionValidator
- •CompareValidator
- •RangeValidator
- •CustomValidator
- •ValidationSummaryx
- •Review
- •Quiz Yourself
- •Maintaining State Out of Process for Scalability
- •No More Cookies but Plenty of Milk!
- •Out of Process State Management
- •Review
- •Quiz Yourself
- •Introducing the Key Security Mechanisms
- •Web.config and Security
- •Special identities
- •Using request types to limit access
- •New Tricks for Forms-based Authentication
- •Using the Passport Authentication Provider
- •Review
- •Quiz Yourself
- •ASP.NET Updates to the ASP Response Model
- •Caching with ASP.NET
- •Page Output Caching
- •Absolute cache expiration
- •Sliding cache expiration
- •Fragment Caching
- •Page Data Caching
- •Expiration
- •File and Key Dependency and Scavenging
- •Review
- •Quiz Yourself
- •A Brief History of Microsoft Data Access
- •Differences between ADO and ADO.NET
- •Transmission formats
- •Connected versus disconnected datasets
- •COM marshaling versus text-based data transmission
- •Variant versus strongly typed data
- •Data schema
- •ADO.NET Managed Provider Versus SQL Managed Provider
- •Review
- •Quiz Yourself
- •Review
- •Quiz Yourself
- •Creating a Connection
- •Opening a Connection
- •Using Transactions
- •Review
- •Quiz Yourself
- •Building a Command
- •Connection property
- •CommandText property
- •CommandType property
- •CommandTimeout property
- •Appending parameters
- •Executing a Command
- •ExecuteNonQuery method
- •Prepare method
- •ExecuteReader method
- •Review
- •Quiz Yourself
- •Introducing DataReaders
- •Using DataReader Properties
- •Item property
- •FieldCount property
- •IsClosed property
- •RecordsAffected property
- •Using DataReader Methods
- •Read method
- •GetValue method
- •Get[Data Type] methods
- •GetOrdinal method
- •GetName method
- •Close method
- •Review
- •Quiz Yourself
- •Constructing a DataAdapter Object
- •SelectCommand property
- •UpdateCommand, DeleteCommand, and InsertCommand properties
- •Fill method
- •Update method
- •Dispose method
- •Using DataSet Objects
- •DataSetName property
- •CaseSensitive property
- •Review
- •Quiz Yourself
- •Constructing a DataSet
- •Tables property
- •TablesCollection Object
- •Count property
- •Item property
- •Contains method
- •CanRemove method
- •Remove method
- •Add method
- •DataTable Objects
- •CaseSensitive property
- •ChildRelations property
- •Columns property
- •Constraints property
- •DataSet property
- •DefaultView property
- •ParentRelations property
- •PrimaryKey property
- •Rows property
- •Dispose method
- •NewRow method
- •Review
- •Quiz Yourself
- •What Is Data Binding?
- •Binding to Arrays and Extended Object Types
- •Binding to Database Data
- •Binding to XML
- •TreeView Control
- •Implement the TreeView server control
- •Review
- •Quiz Yourself
- •DataGrid Control Basics
- •Binding a set of data to a DataGrid control
- •Formatting the output of a DataGrid control
- •Master/Detail Relationships with the DataGrid Control
- •Populating the Master control
- •Filtering the detail listing
- •Review
- •QUIZ YOURSELF
- •Updating Your Data
- •Handling the OnEditCommand Event
- •Handling the OnCancelCommand Event
- •Handling the OnUpdateCommand Event
- •Checking that the user input has been validated
- •Executing the update process
- •Deleting Data with the OnDeleteCommand Event
- •Sorting Columns with the DataGrid Control
- •Review
- •Quiz Yourself
- •What Is Data Shaping?
- •Why Shape Your Data?
- •DataSet Object
- •Shaping Data with the Relations Method
- •Review
- •Quiz Yourself
- •OLEDBError Object Description
- •OLEDBError Object Properties
- •OLEDBError Object Methods
- •OLEDBException Properties
- •Writing Errors to the Event Log
- •Review
- •Quiz Yourself
- •Introducing SOAP
- •Accessing Remote Data with SOAP
- •SOAP Discovery (DISCO)
- •Web Service Description Language (WSDL)
- •Using SOAP with ASP.NET
- •Review
- •Quiz Yourself
- •Developing a Web Service
- •Consuming a Web Service
- •Review
- •Quiz Yourself
- •ASP and ASP.NET Compatibility
- •Scripting language limitations
- •Rendering HTML page elements
- •Using script blocks
- •Syntax differences and language modifications
- •Running ASP Pages under Microsoft.NET
- •Using VB6 Components with ASP.NET
- •Review
- •Quiz Yourself
- •Preparing a Migration Path
- •ADO and ADO.NET Compatibility
- •Running ADO under ASP.NET
- •Early Binding ADO COM Objects in ASP.NET
- •Review
- •Quiz Yourself
- •Answers to Part Reviews
- •Friday Evening Review Answers
- •Saturday Morning Review Answers
- •Saturday Afternoon Review Answers
- •Saturday Evening Review Answers
- •Sunday Morning Review Answers
- •Sunday Afternoon Review Answers
- •What’s on the CD-ROM
- •System Requirements
- •Using the CD with Windows
- •What’s on the CD
- •The Software Directory
- •Troubleshooting
- •ADO.NET Class Descriptions
- •Coding Differences in ASP and ASP.NET
- •Retrieving a Table from a Database
- •Displaying a Table from a Database
- •Variable Declarations
- •Statements
- •Comments
- •Indexed Property Access
- •Using Arrays
- •Initializing Variables
- •If Statements
- •Case Statements
- •For Loops
- •While Loops
- •String Concatenation
- •Error Handling
- •Conversion of Variable Types
- •Index
108 |
Saturday Afternoon |
Note the boldfaced code. We have set the ControlToValidate property to the second password field. Once the user has entered the second password, a client-side validation will compare it against the first password field defined by the ControlToCompare property.
Additionally, we have set the Type property of the comparison to String to insure that when the Operator property Equal is applied that the comparison will work correctly. As already mentioned, you could use any number of operator enumerators to do the comparison as well as any of the property enumerators.
If we wanted to compare a value rather than two controls, you could simply set the ValueToCompare property to a specific string rather than use the ControlToCompare property.
RangeValidator
This control is useful to compare one control to values of two other controls or to a specific range. In Listing 11-4, we are simply checking to see if the user has entered a valid age range of equal or greater than 18 but less than or equal to 50 years old.
Listing 11-4 Using a RangeValidator Control
<%@ Page Language=”vb” %> <HTML>
<HEAD>
<SCRIPT LANGUAGE=”VB” RUNAT=”server”>
Sub Page_Load(Source As Object, E as EventArgs) If Page.IsPostBack Then
lblTitle.Text = “Submit was successful”
Else
lblTitle.Text = “Enter an age <18 or >50 and hit the Submit button”
End If End Sub </SCRIPT>
</HEAD>
<BODY>
<FORM ID=”WebForm1” METHOD=”post” RUNAT=”server” NAME=”WebForm1”> <P>
<ASP:LABEL ID=”lblTitle” RUNAT=”SERVER” /> </P>
<P>
Age
<ASP:TEXTBOX ID=”txtAge” RUNAT=”SERVER” HEIGHT=”24” WIDTH=”28”></ASP:TEXTBOX>
<ASP:RANGEVALIDATOR
ID=”RangeValidator1”
ERRORMESSAGE=”You must be older than 18 and less than 50 to
register”
RUNAT=”SERVER”
CONTROLTOVALIDATE=”txtAge”
BACKCOLOR=”#FFFF80”
MINIMUMVALUE=”18”
TYPE=”Integer”
MAXIMUMVALUE=”50”>
</ASP:RANGEVALIDATOR>
Session 11—Validating User Input |
109 |
</P>
<P>
<ASP:BUTTON ID=”btnSubmit” RUNAT=”SERVER” TEXT=”Submit”></ASP:BUTTON> </P>
</FORM>
</BODY>
</HTML>
Most of the properties on this control we have used in the previous examples. The new property, in boldface, of MinimumValue establishes the minimum range of the compare. MaximumValue establishes the upper range of the comparison. If you wanted to utilize the values of other controls, you could use the properties MaximumControl and MinimumControl, setting the values equal to the id’s of the controls you want to compare against.
Several factors for this control should be noted. If the user leaves a control blank, the control passes the range validation. To force the user to enter a value, add a RequiredField validation control as well. If both MaximumControl and MaximumValue are specified, then the MaximumControl is used. If both MinimumControl and MinimumValue are specified, then MinimumControl will be used to perform the range validation.
CustomValidator
Although the above controls should cover 90 percent of your validation needs, there will be scenarios where you will want to take a value the user enters, apply an algorithm, compare it to a database value, or run it against a Web service to determine if the information is valid. In these cases, you can utilize the CustomValidator control. This control enables you to define both clientand server-side validation routines to compare a control value against. These functions must return Boolean true or false to process the appropriate error message for the control.
For the current example, you are going to compare a subscription code provided by the user against a fixed value. Listing 11-5 illustrates how you can utilize a custom server-side function and a custom client-side function to perform validation.
In Listing 11-5, we have created a server-side function ValidateSubscriptionServer that simply accepts the control value as a string and sets objArgs.IsValid equal to true or false depending upon the result. In this case, you simply check to see if the user control you are validating has a text value equal to abc123. However, you could have also performed a database query or any other type of routine to do the comparison.
Next, we have included a client-side validation routine. The routine is a Javascript 1.0- compliant function called ValidateSubscriptionClient that runs on the client side. Since this function does not have the RunAt = Server attribute, it can have the same name as our server-side function, but will be processed as soon as a user moves their mouse from the Subscription Code field.
Listing 11-5 Using a CustomValidator Control
<%@ Page Language=”vb” %> <HTML>
<HEAD>
<SCRIPT LANGUAGE=”VB” RUNAT=”server”>
Continued
110 |
Saturday Afternoon |
Listing 11-5 |
Continued |
Sub Page_Load(Source As Object, E as EventArgs)
End Sub
Public Sub ValidateSubscriptionServer(objsource As Object, objArgs As ServerValidateEventArgs)
If strComp(objArgs.Value, “abc123”, CompareMethod.Text) = 0 Then objArgs.isValid=True
lblTitle.Text = “Subscription value accepted on client and server!” Else
objArgs.isValid =False
lblTitle.Text = “Subscription value rejected on server!” End If
End Sub </SCRIPT>
<SCRIPT LANGUAGE=”javascript”>
function ValidateSubscriptionClient(objSource,objArgs)
{
if(objArgs.Value==”abc123”)
{
objArgs.IsValid= true;
}
else
{
objArgs.IsValid=false;
}
return;
}
</SCRIPT>
</HEAD>
<BODY>
<FORM ID=”WebForm1” NAME=”WebForm1” METHOD=”post” RUNAT=”server”> <P>
<ASP:LABEL id=”lblTitle” RUNAT=”SERVER”></ASP:LABEL> </P>
<P>
Subscription Code
<ASP:TEXTBOX id=”txtSubscription” RUNAT=”SERVER”></ASP:TEXTBOX>
<ASP:CUSTOMVALIDATOR id=”CustomValidator1” RUNAT=”SERVER” ONSERVERVALIDATE=”ValidateSubscriptionServer” CLIENTVALIDATIONFUNCTION=”ValidateSubscriptionClient” BACKCOLOR=”#FFFF80” CONTROLTOVALIDATE=”txtSubscription” ERRORMESSAGE=”This Subscription Code is Not Valid”>
</ASP:CUSTOMVALIDATOR>
</P>
<P>
<ASP:BUTTON id=”btnSubmit” RUNAT=”SERVER” TEXT=”Submit”></ASP:BUTTON> </P>
</FORM>
</BODY>
</HTML>