- •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
84 |
Saturday Morning |
List Controls
List controls, like intrinsic controls, map closely to HTML elements. The reason they are in their own category is that List controls present the user with a list of options. The general rule here is that there is a parent object that contains multiple child objects. For example a DropDownList control contains one or many List items. The List controls are:
DataGrid
DataList
CheckBoxList
DropDownList
ListBox
RadioButtonList
Note
Since the use of Web controls is uniform regardless of type, I won’t go into too much detail here. We recommend that you look at your .NET documentation to get a complete listing of each List control’s properties, methods, and events.
The following snippet of code illustrates how to create a DropDownList by manually inserting a List Object:
<html>
<head>
</head>
<body>
<form runat=”server”>
<asp:DropDownList id=”cmbPeople” runat=”server”> <asp:ListItem value=”0” text=””/> <asp:ListItem value=”1” text=”Bill Gates”/> <asp:ListItem value=”2” text=”Larry Ellison”/> <asp:ListItem value=”3” text=”Steve Case”/>
</asp:DropDownList>
<br/><br/>
<asp:Button id=”Button1” Text=”Submit” runat=”server”/> </form>
</body>
</html>
This is a simple example, but probably requires a little clarification. First, you’ll notice that we used the following syntax to declare a ListItem:
<asp:ListItem value=”1” text=”Bill Gates”/>
Session 9—Using Web Controls |
85 |
We could have just as easily used:
<asp:ListItem value=”1”>Bill Gates</asp:ListItem>
OK, now let’s try it programmatically:
<script language=”vb” runat=”server”>
Sub Page_Load(Sender As Object, e As EventArgs) cmbPeople.Items.Add(“”) cmbPeople.Items.Add(“Bill Gates”) cmbPeople.Items.Add(“Larry Ellison”) cmbPeople.Items.Add(“Steve Case”)
End Sub </script> <html> <head> </head> <body>
<form runat=”server”>
<asp:DropDownList id=”cmbPeople” runat=”server”/> <br/><br/>
<asp:Button id=”Button1” Text=”Submit” runat=”server”/> </form>
</body>
</html>
List controls can be bound to a data source very easily. We’ll introduce data binding in Session 22, “Introducing Data Binding.”
Cross-Ref
Rich Controls
Rich controls are very different from intrinsic and List Web controls. Intrinsic and List controls can roughly be traced to a single HTML element. Rich controls provide a piece of functionality that requires the use of multiple HTML elements. The ASP.NET rich controls are
AdRotator
Calendar
In the “old ASP world,” developers would have either (1) written a lot of HTML/ASP code or (2) written an ActiveX control to provide the functionality that is now provided by ASP.NET rich controls. The nice thing is that all state maintenance is managed for us and the user doesn’t need to download a component. Very convenient! There should soon be quite an aftermarket for custom rich controls.
86 |
Saturday Morning |
Listing 9-4 demonstrates the ease-of-use of the Calendar control.
Listing 9-4 Using the Calendar control
<html>
<head>
<script language=”VB” runat=”server”>
Sub Calendar_Change(Source As Object, E As EventArgs) If Page.IsPostBack Then
lblMessage.Text = “You selected “ & ctlCalendar.SelectedDate.ToLongDateString()
End If End Sub </script>
</head>
<body>
<form id=”frmCalendar” runat=”server”> <asp:Label id=”lblMessage” runat=”server” /> <br/><br/>
<asp:Calendar id=”ctlCalendar” BackColor=”white”
BorderWidth=”3”
BorderStyle=”Solid”
BorderColor=”Black”
CellSpacing=”2”
CellPadding=”2”
ShowGridLines=”True” TitleStyle-BackColor=”white” TitleStyle-ForeColor=”black” DayHeaderStyle-ForeColor=”white” DayStyle-ForeColor=”black” SelectedDayStyle-BackColor=”red” OnSelectionChanged=”Calendar_Change”
runat=”server” /> </form>
</body>
</html>
If you view the source for output, you’ll see that Microsoft has really done us a favor by providing rich controls. The great thing about rich controls is that they are infinitely customizable.
Cross-Ref
The fourth category of HTML controls, Validation controls, deserves a session to itself. So, we’ll be discussing those in Session 11, “Validating User Input.”
Session 9—Using Web Controls |
87 |
REVIEW
Web controls are server-side ASP.NET controls that render browser-specific HTML Web controls not only reduce the time required to develop HTML that will correctly render on different browsers, but Web controls can be used to maintain state between server requests. Web controls also allow us, as developers, to capture client-side events and process them on the server.
QUIZ YOURSELF
1.What advantages do Web controls provide over HTML controls? (See session introduction.)
2.What are the four basic categories of Web controls? (See session introduction.)
3.(True/False) Web controls map one-to-one with HTML elements. (See session introduction.)
S E S S I O N
10
Introducing User Controls
Session Checklist
Understanding the importance of User Controls in the .NET Framework
Learning to create a User Control
After working through the previous two sessions, you should have a good grasp on the controls, HTML and Web, which are provided with ASP.NET. Although the controls packaged with ASP.NET are very useful, chances are you will run into a situation
where creating a custom control might be a good idea. Why? First, the standard ASP.NET controls are developed to meet the most common functional requirements; they were not designed to meet every requirement, or very specific requirements. If an HTML or Web control meets one of your functional requirements but only in a general way, you could end up writing a ton of code in your ASP.NET pages to customize it. Furthermore, if that functionality is required on multiple pages (which from our experience is quite common), maintenance could turn out be a major headache. Second, your pages could require the combination of several ASP.NET controls. Again, if you code this functionality into each ASP.NET page, you’re really shooting yourself in the foot. Maintainability! Maintainability! Maintainability!
Both of these common situations can be addressed with the use of User Controls. User Controls provide an easy way to partition and reuse simple, common user interface (UI) functionality across a Web application. Furthermore, User Controls are compiled on demand and cached in server memory so you can gain a bit of a performance boost. User Controls do not need to be authored in the same language as the ASP.NET page in which they are being included. For example, if one developer is creating an ASP.NET using Visual Basic,
he or she can include a User Control written in C# or C++. From a business perspective, allowing developers to code in the language with which they’re most fluent can drastically improve performance. Plus, your resource pool broadens.