Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
ASP .NET Database Programming Weekend Crash Course - J. Butler, T. Caudill.pdf
Скачиваний:
31
Добавлен:
24.05.2014
Размер:
3.32 Mб
Скачать

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.