- •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
80 |
Saturday Morning |
More flexible controls. Some controls offer the ability to specify whether a control’s event causes immediate posting to the server or whether it is cached and raised when the form is submitted.
Better communication between controls. This includes the ability to pass events from a nested control (such as a button in a table) to the container control.
At this point, you may be wondering why Microsoft opted to offer both HTML and Web controls. The answer is simple: flexibility. You can use whichever set of controls you feel more comfortable with. HTML controls keep you closer to the content. By contrast, Web controls provide a more consistent programming model, but distance you a little from the actual output.
You use Web controls in the same way that you use HTML controls. The only difference is that they must have the runat=”server” name/value pair. You don’t have to do anything special to access this code library as it’s available by default, but you do have to ensure you use the correct tag prefix (or namespace) when using the controls.
In general, Web controls can be grouped into one of four basic categories:
Intrinsic controls
List controls
Rich controls
Validation controls
Intrinsic Controls
The intrinsic controls are designed to provide replacements for the standard HTML controls. Here is a list of the intrinsic controls:
Button |
CheckBox |
Hyperlink |
Image |
Label |
LinkButton |
Panel |
Table |
TableCell |
TableRow |
TextBox |
|
Using intrinsic controls
We are really fond of Web controls. They are easy to use and immensely programmable. Each control is an object and therefore has its own set of properties, methods, and events. We have found that using Web controls greatly eases the pain of writing repetitive HTML code. You may not feel as closely connected to the HTML when using Web controls, but at least you’ll know that your page will render correctly regardless of which browser is used.
Here is a code sample that creates an HTML table using the ASP.NET Table Web control:
<html>
<head>
</head>
Session 9—Using Web Controls |
81 |
<body>
<asp:Table id=”tblExample” BorderWidth=1 GridLines=”both” runat=”server”/> </body>
</html>
When you run the page, you’ll notice that nothing is displayed. That’s because we haven’t added any cells to the data. By examining the HTML output from the page, you should, however, see the HTML table. Here is the HTML generated in IE 5.5:
<html>
<head>
</head>
<body>
<table id=”tblExample” rules=”all” border=”1” style=”border-width:1px;border-style:solid;”> </table>
</body>
</html>
If we further examine the HTML output, we see that there is some HTML that we didn’t add. For example, the style and border attributes were created for us by the ASP.NET engine based on the properties we set for the Table Web control (here: BorderWidth and GridLines). This is how browser compatibility is handled. The ASP.NET engine sniffs the browser to determine its capabilities and sends HTML that the browser can handle. This is a simple operation, but it’s really a pain if you’re forced to do it yourself.
Next, you’ll expand on the previous sample page by adding a few rows and columns to the table. There are two ways to accomplish this: you can add TableRow and TableCell Web controls (a) manually or (b) programmatically. Listing 9-1 shows the manual approach.
Listing 9-1 Using intrinsic controls (manually)
<html>
<head>
</head>
<body>
<asp:Table id=”tblExample” BorderWidth=1 GridLines=”both” runat=”server”> <asp:TableRow>
<asp:TableCell>Row 1, Cell 1</asp:TableCell> <asp:TableCell>Row 1, Cell 2</asp:TableCell> <asp:TableCell>Row 1, Cell 3</asp:TableCell> <asp:TableCell>Row 1, Cell 4</asp:TableCell> <asp:TableCell>Row 1, Cell 5</asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell>Row 2, Cell 1</asp:TableCell> <asp:TableCell>Row 2, Cell 2</asp:TableCell> <asp:TableCell>Row 2, Cell 3</asp:TableCell> <asp:TableCell>Row 2, Cell 4</asp:TableCell> <asp:TableCell>Row 2, Cell 5</asp:TableCell>
</asp:TableRow>
</asp:Table>
</body>
</html>
82 |
Saturday Morning |
You’ll notice that all we are doing here is creating rows using the TableRow Web control and cells using the TableCell Web control. There are two things we would like to mention here. First, the Web controls must be formed correctly, which means that if we open, for example, a TableCell, we must close it using the following (XML) syntax:
</asp:TableCell>
Secondly, it isn’t necessary to include the runat=”server” attribute/value pair when creating the TableRow and TableCell Web controls in the example because they belong to the Table Web control that did include the runat=”server” attribute/value pair. As a rule, you should always include it so there’s no confusion about what you’re doing. (We didn’t include the runat=”server” attribute/value pair for demonstration purposes only.)
Manually adding rows and cells is great if you’re using the table for formatting and know exactly how many rows and cells you need. In many cases, however, you don’t have this information so it may be better to take the programmatic approach. Listing 9-2 shows the code listing that, when run, creates 10 rows and 50 cells programmatically.
Listing 9-2 Using intrinsic controls (programmatically)
<script language=”VB” runat=”server”>
Sub Page_Load(Sender As Object, E As EventArgs)
Dim |
iRowCount As |
Integer |
‘ Current |
row count |
Dim |
iColumnCount |
As Integer |
‘ Total |
number of columns (columns) |
For iRowCount = 1 To 10
Dim tRow As New TableRow()
For iColumnCount = 1 To 5
Dim tCell As New TableCell()
tCell.Text = “Row “ & iRowCount & “, Cell “ & iColumnCount tRow.Cells.Add(tCell) ‘ Add new TableCell object to row
Next tblExample.Rows.Add(tRow)
Next
End Sub </script> <html> <head> </head> <body>
<asp:Table id=”tblExample” BorderWidth=1 GridLines=”both” runat=”server”/> </body>
</html>
In the body of the HTML, we declare a Table Web control. Since we are not initially declaring any TableRows or TableCells, we end the Table declaration with /> rather than >.
We could have just as easily closed the Table Web control using the </asp:Table> syntax.
Again, a matter of personal preference! At the beginning of the page, we have included a
Session 9—Using Web Controls |
83 |
simple script within the Page_Load event that adds rows and cells rows to the table programmatically. Every time the page is called, this script will be executed. Because this is not a book about VB.NET, I won’t go into the syntax of the script. The important thing to realize is that by using an object’s properties, methods, and events, you can programmatically create other objects at runtime.
Handling intrinsic Web control events
Now may be as good a time as any to talk about handling Web control events. All ASP.NET events are handled on the server rather than the client. This is kind of a new way of thinking for many developers who are used to writing client-side code, but it has the advantage of providing cross-browser compatibility. Each and every Web control has its own set of events. You’ll have to refer to your ASP.NET documentation for a complete listing of each control’s events. For example, the ASP.NET Button Web control has an OnClick event that is fired when the button is clicked. Listing 9-3 illustrates handling an OnClick event.
Listing 9-3 Handling OnClick events
<script language=”VB” runat=”server”>
Sub btnTest_Click(Sender As Object, E As EventArgs) If tblExample.Rows.Count = 0 Then
Dim |
iRowCount As |
Integer |
‘ Current |
row count |
Dim |
iColumnCount |
As Integer |
‘ Total |
number of cells (columns) |
For iRowCount = 1 To 10
Dim tRow As New TableRow()
For iColumnCount = 1 To 5
Dim tCell As New TableCell()
tCell.Text = “Row “ & iRowCount & “, Cell “ & iColumnCount tRow.Cells.Add(tCell) ‘ Add new TableCell object to row
Next tblExample.Rows.Add(tRow)
Next End If
End Sub </script> <html> <head> </head> <body>
<asp:Table id=”tblExample” BorderWidth=1 GridLines=”both” runat=”server”/> <form runat=”server”>
<asp:Button id=”btnTest” OnClick=”btnTest_Click” Text=”Insert Rows” runat=”server”/>
</form>
</body>
</html>