- •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
64 |
Saturday Morning |
the page is loaded again. The word “True” now appears above the Submit button. That is because the page is being loaded in response to a post back to the server.
Now, try adding some code to the Control_Click event handler and wiring the Submit button to fire the Control_Click event handler as shown in Listing 7-2.
Listing 7-2 Using the Control_Click event handler
<SCRIPT LANGUAGE=”VB” RUNAT=”server”>
Sub Page_Load(Source As Object, E As EventArgs) ‘ Page_Load Code
lblTest.Text = Page.isPostBack End Sub
Sub Control_Click(Sender As Object, E As EventArgs) ‘Control_Click Code
Response.Write(“The Submit button was clicked!”) End Sub
Sub Page_Unload(Source As Object, E As EventArgs) ‘ Page_Unload
End Sub </SCRIPT> <html> <head>
<title>ASP.NET Page</title> </head>
<body>
<form ID=”frmTest” RUNAT=”SERVER”> <asp:Label ID=”lblTest” RUNAT=”SERVER”/> </br>
<asp:Button ID=”btnSubmit” onClick=”Control_Click” TEXT=”Submit” RUNAT=”SERVER”/>
</form>
</body>
</html>
Take a look at the Button Web control declaration. You’ll notice that we added the onClick=”Control_Click” attribute/value pair. This declaration wires the btnSubmit button to fire the Control_Click event handling routine.
Now take a look at the Control_Click event handling method. When fired, by the btnSubmit Web control, the phrase “The Submit button was clicked!” will be written to the page response. Try running the page to see what happens.
Page Directives
ASP.NET pages can optionally contain directives that specify settings to be used by the page compiler. Page directives can be located anywhere within an .aspx file. Additionally each directive can contain multiple attribute/value pairs specific to the directive. The syntax for a page directive is:
<%@ directive attribute=”value” [attribute=”value” . . .]%>
Session 7—Developing ASP.NET Pages |
65 |
Table 7-1 lists the directives that are supported by ASP.NET pages.
Table 7-1 ASP.NET Page Directives
Directive |
Description |
@Page |
The @Page directive defines page-specific attributes used by the ASP.NET |
|
page parser and compiler. An example of a @Page attribute is Language, |
|
which specifies the default language for the page. |
|
|
@Control |
The @Control directive defines control-specific attributes used by the |
|
ASP.NET page parser and compiler. An example of a @Control attribute |
|
is Description, which provides a text description of the control. |
|
|
@Import |
The @Import directive explicitly imports a namespace into a page. The |
|
only attribute supported by the @Import directive is Namespace, which |
|
indicates the name of namespace to import. (More on namespaces later.) |
|
|
@Register |
The @Register directive associates aliases with namespaces and class |
|
names for concise notation in custom server control syntax. For more |
|
information on the @Register directive, see Session 10. |
|
|
@Assembly |
An assembly is a unit of reusable code compiled into a .dll file. The |
|
@Assembly directive links an assembly against the current page, making |
|
all of the assembly’s classes and interfaces available for use on the page. |
|
The only attribute supported by the @Assembly directive is |
|
Assemblyname, which indicates the name of the assembly to link. |
|
Assemblies that reside in an application \bin directory are automati- |
|
cally linked to pages within the application, therefore, they do not need |
|
to be linked using the @Assembly directive. |
|
|
@OutputCache |
The @OutputCache directive controls the output caching policy for the |
|
page. An example of a @OutputCache attribute is Duration, which |
|
specifies the time (in seconds) that the output cache for the page will |
|
be maintained. |
|
|
Listing 7-3 shows an example of an ASP.NET page with page directives.
Listing 7-3 An ASP.NET page with page directives
<%@ Page Language=”VB” Description=”ASP.NET Page” %> <%@ Import Namespace=”System.Net” %>
<SCRIPT LANGUAGE=”VB” RUNAT=”server”>
Sub Page_Load(Source As Object, E As EventArgs) ‘ Page_Load Code
lblTest.Text = Page.isPostBack End Sub
Continued
66 |
Saturday Morning |
Listing 7-3 |
Continued |
Sub Control_Click(Sender As Object, E As EventArgs) ‘Control_Click Code
Response.Write(“The Submit button was clicked!”) End Sub
Sub Page_Unload(Source As Object, E As EventArgs) ‘ Page_Unload
End Sub </SCRIPT> <html> <head>
<title>ASP.NET Page</title> </head>
<body>
<form ID=”frmTest” RUNAT=”SERVER”> <asp:Label ID=”lblTest” RUNAT=”SERVER”/> </br>
<asp:Button ID=”btnSubmit” onClick=”Control_Click” TEXT=”Submit” RUNAT=”SERVER”/>
</form>
</body>
</html>
Our ASP.NET page now contains three directives: @Page, @Import, and @OutputCache.
Namespaces
As we have mentioned several times in this book, ASP.NET, and actually the .NET Framework, is a hierarchy of classes providing basic services. In order to gain access to these classes or services, you need to import their namespace into the ASP.NET page.
Table 7-2 lists several of the namespaces that are automatically imported into all pages.
Table 7-2 Namespaces That Are Automatically Imported
Namespace |
Description |
System |
Contains fundamental classes and base classes that define |
|
commonly-used value and reference data types, events and |
|
event handlers, interfaces, attributes, and processing |
|
exceptions. |
|
|
System.Collections |
Contains interfaces and classes that define various collec- |
|
tions of objects, such as lists, queues, arrays, hash tables, |
|
and dictionaries. |
|
|
System.IO |
Provides access to the File and Directory objects, which |
|
enable you to add, move, change, create, or delete folders |
|
(directories) and files on the Web server. |
|
|
Session 7—Developing ASP.NET Pages |
67 |
Namespace |
Description |
System.Web |
Includes the HTTPRequest class that provides extensive |
|
information about the current HTTP request, the |
|
HTTPResponse class that manages HTTP output to the |
|
client, and the HTTPServerUtility object that provides |
|
access to server-side utilities and processes. System.Web |
|
also includes classes for cookie manipulation, file transfer, |
|
exception information, and output cache control. |
|
|
System.Web.UI |
Contains the ASP.NET control classes. |
|
|
System.Web.UI.HtmlControls |
Contains the HTML server controls. |
|
|
System.Web.UI.WebControls |
Contains the Web controls. |
|
|
For a list of all of the namespaces imported into ASP.NET pages by default, refer to your ASP.NET documentation.
Note
Table 7-3 explains some other commonly used namespaces.
Table 7-3 Other Commonly Used Namespaces
Namespace |
Description |
System.Data |
Provides access to general data access services. |
|
|
System.Data.OleDb |
Provides access to OLEDB -specific data access services. |
|
|
System.Data.SQLClient |
Provides access to SQL Server data access services. |
|
|
SystemXML |
Provides access to the services for manipulating XML. |
|
|
So, for example, if you wanted to write a page that would be used to access a SQL Server database, you would include the following @Import page directives in your ASP.NET page:
<%@ Import Namespace=”System.Data” %>
<%@ Import Namespace=”System.Data.SqlClient” %>
Choosing a Language
In this session, we have used Visual Basic.NET to write the ASP.NET pages. One of the most attractive features about the .NET Framework is that it is language neutral. This means that any language that provides a .NET compiler can be used to write ASP.NET pages. The number of languages supported is continuously growing. More than likely, at some point, your favorite language will be supported. And since all code is compiled into intermediate language (IL) code, there is no performance penalty or gain for using one language rather than another.