- •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
222 |
Sunday Morning |
REVIEW
Wow! We covered a ton of material in this session, including DataTable objects, DataRow objects, and DataColumn objects. All of these objects constitute a DataSet. Without them, a DataSet would be fairly useless. We could even say that the true power of the DataSet actually lies in its constituent objects. Although in this session, we were not able to cover all of the properties and methods of all of the objects that make up a DataSet, you gained a clearer understanding of the DataSet object model and how to work with it. The most important concept to take away from this session is that a DataSet object is effectively a set of objects, each with their own properties and methods, which are grouped together in a hierarchical fashion to provide you, as a developer, with a robust model through which you can access your data store.
QUIZ YOURSELF
1.What is a collection? (See “Tables property.”)
2.How is a DataTable’s Columns property related to a DataColumnCollection object? (See “Columns property.”)
3.Through which DataTable properties can you gain access to the DataRows in that table? (See “Rows property.”)
S E S S I O N
22
Introducing Data Binding
Session Checklist
Understanding basic binding techniques
Connecting ASP.NET Controls to data stores
Binding the TreeView Control to an XML File
OK, so you now understand all of the various ADO.NET objects, methods, and properties, which provide you a ton of flexibility in handling disconnected data; but now you want to know how to bind all of those great ASP.NET controls to your data
objects, right? Well, this is the session you have been waiting for! We’ll dive into the process of connecting our data stores with the basic server controls, and explore how to bind an XML file to the very useful Treeview control.
What Is Data Binding?
Data binding is the process of connecting a server control to a dataset. Data binding greatly simplifies the amount and complexity of code required to generate basic output such as filling a drop-down list box with a set of names and values stored in a database, XML file, array, hash table or even a custom object. By filtering criteria based upon previous selections, data binding enables you to provide user interfaces that are more intuitive. It also assists in separating your code from your content.
If you have worked with ASP previously, then you probably are familiar with using the RecordSet object to loop through a set of data and then manually build a dynamic dropdown list or table using the result set. You may have even used Visual Interdev and the Design Time Controls (DTC) to bind a RecordSet object to the Grid, Label, Checkbox, Option Group, and other Visual Interdev DTCs. Most likely you also experienced the pain and frustration of attempting to debug the DTCs once they were implemented. Even the simplest DTC
224 |
Sunday Morning |
must have produced about 400 lines of code in your ASP Page, providing a painful and arduous debugging regime for even the most dedicated professional. With this complexity, you may have returned to the old Notepad build-it-yourself approach to reduce your late night troubleshooting sessions!
ASP.NET and ADO.NET now provide the flexibility of the build-it-yourself approach to connecting result sets to HTML objects without the headaches of the old DTC objects.
Binding to Arrays and Extended Object Types
The simplest example of data binding can be illustrated using array bound controls. Listing 22-1 demonstrates binding a simple combo-box control to an array. The first step is
to establish an array structure. You can do this by declaring a new ArrayList and adding a list of values to it as follows.
Listing 22-1 Example of binding arrays to server controls
<%@ Page Language=”VB” Debug=”False” %> <HTML>
<SCRIPT LANGUAGE=”VB” RUNAT=”Server”>
Sub Page_Load(Sender as Object, E as EventArgs) If Not IsPostback Then
‘Dim and fill Array
Dim aList as New ArrayList With aList
.Add(“Model 300 Skis”)
.Add(“Model 1300 Skis”)
.Add(“Model 2300 Skis”)
.Add(“Model 3300 Skis”) End With
dbox1.DataSource = aList dbox1.DataBind()
End If End Sub
Sub dBox1_SelectedIndexChanged(sender As Object , e As System.EventArgs) Response.Write (dbox1.SelectedItem.Value.ToString())
End Sub </SCRIPT> <BODY>
<FORM RUNAT=”Server” METHOD=”post” ID=”Form1”>
<ASP:DROPDOWNLIST ID=”dBox1” RUNAT=”Server” AUTOPOSTBACK=”true”
ONSELECTEDINDEXCHANGED=”dBox1_SelectedIndexChanged” /> </FORM>
</BODY>
</HTML>
Once the ArrayList is populated, you simply bind it to your selected control, in this example a DropDownList with an id of dBox1. You perform the binding by setting the Control.DataSource() method equal to the array and then using the Control.DataBind() method to bind the array to the control.
Session 22—Introducing Data Binding |
225 |
One of the issues with this approach is that both the value and the text are going to be the same. So, what if you want to establish an array that had a bound value and bound text that are different? You can also bind server controls to other objects such as custom classes, hash tables, and of course ADO.NET objects. Let’s modify the code in Listing 22-1 so that we use a custom class called Ski that stores the product id and the product title for a set of skis. You can create an array of classes and then bind the control to the various properties of the class. This example illustrates how you can take just about any conforming class and bind its data to the server controls.
Building on the code in Listing 22-1, you simply create a new class called Ski that accepts a product id and product title when instantiated as shown in Listing 22-2. Additionally it supports ReadOnly properties to allow the return of the ProductId and
ProductTitle values:
Listing 22-2 Custom class for binding to Webserver controls
Public Class Ski
Private _ProductId as Integer
Private _ProductTitle as String
Public Sub New(ByVal i as Integer, ByVal s as String)
_ProductId= i
_ProductTitle = s
End Sub
Public Overridable ReadOnly Property ProductId()
Get
Return _ProductId
End Get
End Property
Public Overridable ReadOnly Property ProductTitle()
Get
Return _ProductTitle
End Get
End Property
End Class
Then you simply modify the way in which you populate the array as shown in this example:
Sub Page_Load(Sender as Object, E as EventArgs) If Not IsPostback Then
Dim aList as New ArrayList() With aList
.Add(new Ski(1001, “Model 300 Skis”))
.Add(new Ski(1002, “Model 1300 Skis”))
.Add(new Ski(1003, “Model 2300 Skis”))
.Add(new Ski(1004, “Model 3300 Skis”)) End With
dbox1.DataSource = aList dbox1.DataBind()
End If
End Sub