- •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
74 |
Saturday Morning |
Hmmmm . . . That’s weird. Where did all of this extra source code come from, and what function does it perform? Let us explain . . .
How HTML controls work
All of the HTML code that was added came from the ASP.NET engine. Each of these additions are being used by the ASP.NET engine to maintain state across client requests. Absolutely no state is maintained on the server using session variables — definitely a plus for scalability.
The hidden __VIEWSTATE field is used to maintain control state and the value is actually a compressed and encrypted value. Typically, you probably won’t be able to make much sense of it. But that’s OK because ASP.NET handles all of the details for you.
Cross-Ref
The __VIEWSTATE form field is used to maintain control state, not user state. User state management will be discussed in Session 12, “Maintaining State in ASP.NET.”
When an ASP.NET page is requested from the server, several things happen relating to HTML controls. First, the aspx page tries to determine whether the page is a post back. If it is, the __VIEWSTATE property is examined, the posted data is processed, and state is applied to the forms elements. All the __VIEWSTATE field does is contain data about control state when the HTML page is generated.
Put in very simplistic terms, this is what happens. No magic, just some processing that is transparent to the developer.
Intrinsic HTML controls
As demonstrated in the previous example, the HTML select object can be used as an ASP.NET HTML control. So you may be wondering what other elements can be ASP.NET HTML controls. Each of the following elements can be used as HTML controls:
<form> |
<td> |
<select> |
<th> |
<img> |
<a> |
<textarea> |
<button> |
<table> |
<tr> |
<input> (checkbox, image, hidden, file, button, text, submit, radio button)
HTML Control Events
Handling HTML control events is a straightforward process. We can use one of two approaches to handling events:
1.Utilize the ASP.NET Page_Load event.
2.Create custom event handlers.
Session 8—Using HTML Controls |
75 |
ASP.NET’s Page object provides you with a facility for handling events on the server side using the Page_Load event. Handling events using the Page_Load event requires you to write some code that first checks to see if the request is a post back and then performs the appropriate actions.
The Page_OnLoad event
Listing 8-4 demonstrates how you can handle HTML control events with the Page_OnLoad event.
Listing 8-4 Using HTML control events with the Page_OnLoad event
<script runat=”server” language=”VB”>
Sub Page_Load(Sender As Object, E As EventArgs) If Page.IsPostBack Then
Select cmbPeople.value Case “Bill Gates”
Response.Redirect(“http://www.microsoft.com”) Case “Larry Ellison”
Response.Redirect (“http://www.oracle.com”) Case “Steve Case”
Response.Redirect (“http://www.aol.com”) Case Else
End Select End If
End Sub </script> <html> <body>
<form id=”frmPeople” method=”post” runat=”server”> People<br>
<select id=”cmbPeople” runat=”server”> <option></option>
<option>Bill Gates</option> <option>Larry Ellison</option> <option>Steve Case</option>
</select><br>
<input type=”submit” value=”Submit”> </form>
</body>
</html>
You’ll notice at the top of this page a function called Page_Load is invoked. This function is called each time the page is requested by a client. You must check to see whether the page request is a post back (that is, a form has been submitted) by using the Page object’s IsPostBack property. If the IsPostBack property returns true, you can check the submitted values — in this case, the value of the cmbPeople select element.
With ASP.NET we can check the value of a form element using its value property. We no longer need to use the Request.Form syntax.
Note
76 |
Saturday Morning |
The rest of the code is straightforward. Use Visual Basic’s Select control structure to redirect the user to a Web site depending on the value selected. Remember that this code is being handled on the server side, so the browser used by the client is inconsequential.
Custom event handlers
In order to create a custom event handler, you need to do two things:
Create a subroutine that will act as the event handler.
Wire an HTML control to call the event handler on the server side.
In the following example, we will create a subroutine called Sample_Handler to handle event processing. This subroutine will be called by the Submit button by simply adding the following the runat=”server” and onserverclick=”Sample_Handler” attribute/value pairs to the control declaration as follows:
<input type=”submit” value=”Submit” id=”smbSubmit” runat=”server” onserverclick=”Sample_Event”>
That’s all you have to do. Listing 8-5 shows the entire page:
Listing 8-5 Using a custom event handler
<script runat=”server” language=”VB”>
Sub Sample_Handler(Sender As Object, E As EventArgs) Select cmbPeople.value
Case “Bill Gates”
Response.Redirect (“http://www.microsoft.com”) Case “Larry Ellison”
Response.Redirect (“http://www.oracle.com”) Case “Steve Case”
Response.Redirect (“http://www.aol.com”) Case Else
End Select End Sub </script> <html> <body>
<form id=”frmPeople” method=”post” runat=”server”> People<br>
<select id=”cmbPeople” runat=”server”> <option></option>
<option>Bill Gates</option> <option>Larry Ellison</option> <option>Steve Case</option>
</select><br>
<input type=”submit” value=”Submit” id=”cmbSubmit” runat=”server” onserverclick=”Sample_Handler”>
</form>
</body>
</html>