Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Pro ASP.NET 2.0 In CSharp 2005 (2005) [eng]

.pdf
Скачиваний:
92
Добавлен:
16.08.2013
Размер:
29.8 Mб
Скачать

98 CHAPTER 3 WEB FORMS

Figure 3-11. Writing custom trace messages

Application Tracing

By default, tracing is enabled on a page-by-page basis. This isn’t always convenient. In some cases, you want to collect trace statistics for a page and then view them later. ASP.NET supports this approach with application-level tracing.

To enable application-level tracing, you need to modify the web.config configuration file. Look for the <trace> element and enable it as shown here:

<configuration>

<system.web>

<trace enabled="true" requestLimit="10" pageOutput="false" traceMode="SortByTime" localOnly="true" />

</system.web>

</configuration>

When you enable application-level tracing, you won’t see the trace information on the page. Instead, to view tracing information you must request the trace.axd application extension in your web application’s root directory. This extension doesn’t correspond to an actual file—instead, ASP.NET automatically intercepts the request and lists the most recently collected trace requests (as shown in Figure 3-12), provided you’re making the request from the local machine or have enabled remote tracing. You can see the detailed information for any request by clicking the View Details link.

CHAPTER 3 WEB FORMS

99

Figure 3-12. Traced application request

Table 3-6 describes the full list of tracing options in the web.config <trace> element.

Table 3-6. Tracing Options

Attribute

Values

Description

Enabled

true, false

Turns tracing on or off for all pages. This is the

 

 

default setting for your web application—you

 

 

can still override it on a page-by-page basis

 

 

with the Page directive. Use the pageOutput

 

 

setting to determine whether trace

 

 

information is shown in the page or collected

 

 

silently.

traceMode

SortByTime,

Determines the sort order of trace messages.

 

SortByCategory

 

localOnly

true, false

Determines whether tracing information will

 

 

be shown only to local clients (clients using

 

 

the same computer) or can be shown to

 

 

remote clients as well. By default, this is true

 

 

and remote clients cannot see tracing

 

 

information. In a production-level application,

 

 

this should always be true to ensure security.

pageOutput

true, false

Determines whether tracing information will

 

 

be displayed on the page (as it is with page-

 

 

level tracing) or just stored on the server

 

 

(application-level tracing). If you choose false

 

 

to use application-level tracing, you’ll still be

 

 

able to view the collected information by

 

 

requesting trace.axd from the virtual directory

 

 

where your application is running.

Continued

100 CHAPTER 3 WEB FORMS

Table 3-6. Continued

Attribute

Values

Description

requestLimit

Any integer

When using application-level tracing, this is

 

(for example, 10)

the number of HTTP requests for which

 

 

tracing information will be stored. Unlike

 

 

page-level tracing, this allows you to collect a

 

 

batch of information from multiple requests.

 

 

If you specify any value greater than 10,000,

 

 

ASP.NET treats it as 10,000. When the

 

 

maximum is reached, the behavior depends

 

 

on the value of the mostRecent setting.

mostRecent

true, false

If true, ASP.NET keeps only the most recent

 

 

trace messages. When the requestLimit

 

 

maximum is reached, the information for the

 

 

oldest request is abandoned every time a new

 

 

request is received. If false (the default),

 

 

ASP.NET stops collecting new trace messages

 

 

when the limit is reached and ignores

 

 

subsequent requests.

writeToDiagnosticsTrace

true, false

If true, all trace messages are also forwarded to

 

 

the System.Diagnostics tracing infrastructure

 

 

and received by any trace listeners you’ve

 

 

configured using that model. The default is

 

 

false. The System.Diagnostics trace features

 

 

are not ASP.NET-specific and can be used in a

 

 

wide variety of .NET applications. They may

 

 

be used in ASP.NET as a way to automatically

 

 

capture trace messages and enter them in an

 

 

event log.

 

 

 

Tracing with the ASP.NET Development Helper

If you’ve installed the ASP.NET Development Helper introduced in Chapter 2 (and available at http://www.nikhilk.net/ASPNETDevHelperTool.aspx), you have another option for looking at tracing information—viewing it in a separate window. When the ASP.NET Developer Helper is running (both on the web server and web browser), it automatically removes trace information from the page. To access it, you can either uncheck the Hide Trace from Page check box (which shows it in the page) or click the Show Trace link.

Figure 3-13 shows this handy feature at work.

Accessing the HTTP Context in Another Class

Over the past several sections, you’ve seen how the Page class exposes a significant number of useful features that let you retrieve information about the current HTTP context. These details are available because they’re provided as properties of the Page class. But what if you want to retrieve this information from inside another class, one that doesn’t derive from Page?

Fortunately, another way exists to get access to all the HTTP context information. You can use the System.Web.HttpContext class. This class exposes a static property called Current, which returns an instance of the HttpContext class that represents all the information about the current request and response. It provides the same set of built-in ASP.NET objects as properties.

CHAPTER 3 WEB FORMS

101

Figure 3-13. Managing trace information with the ASP.NET Development Helper

For example, here’s how you would write a trace message from another component that doesn’t derive from Page but is being used by a web page as part of a web request:

HttpContext.Current.Trace.Write("This message is from DB Component");

If you want to perform multiple operations, it may be slightly faster to retrieve a reference to the current context and then reuse it:

HttpContext current = HttpContext.Current; current.Trace.Write("This is message 1"); current.Trace.Write("This is message 2");

Summary

In this chapter you walked through a detailed examination of the ASP.NET page. You learned what it is and how it really works behind the scenes with postbacks and view state. You also learned the basics of the server control model, took a close look at the System.Web.UI.Page class, and learned how to use tracing. In the next chapter, you’ll take a closer look at the web controls that ASP.NET gives you to build sophisticated pages.

C H A P T E R 4

■ ■ ■

Server Controls

ASP.NET server controls are a fundamental part of the ASP.NET architecture. Essentially, server controls are classes in the .NET Framework that represent visual elements on a web form. Some of these classes are relatively straightforward and map closely to a specific HTML tag. Other controls are much more ambitious abstractions that render a more complex representation from multiple HTML elements.

In this chapter, you’ll learn about the different types of ASP.NET server controls and how they’re related. You’ll also learn how to use validation controls to ensure that the user input matches specific rules before a web page is submitted to the server.

SERVER CONTROLS CHANGES IN .NET 2.0

ASP.NET 2.0 keeps the .NET 1.0 controls almost unchanged and adds a slew of new controls. However, you won’t learn about these new controls in this chapter. Instead, you’ll explore them as you examine various topics. For example, when you consider data binding, you’ll learn about the GridView and DetailsView (Chapter 9 and Chapter 10); when you explore navigation, you’ll use the TreeView and Menu (Chapter 16); and when you create web portals, you’ll use the new portal controls (Chapter 31).

In this chapter, you’ll consider ASP.NET control basics. There are just a few new features, as listed here (in the order they appear in this chapter):

Control focus: You can now programmatically set the ASP.NET control that will have focus when the page is displayed in the browser.

Default buttons: You can now set the ASP.NET control that will be triggered when the user presses the Enter key while viewing a page.

BulletedList: ASP.NET 2.0 provides a server-side abstraction for HTML ordered and unordered lists with the new BulletedList control.

Validation groups: Rather than validate the entire page in an all-or-nothing operation, you can organize controls into logical groups that are validated independently.

Also, a few new features are built into the base control and web-page classes that you won’t study in this chapter. One notable example is themes (Chapter 15).

103

104 C H A P T E R 4 S E R V E R C O N T R O L S

Types of Server Controls

ASP.NET offers many different server controls, which fall into several categories. This chapter explores the controls in the following categories:

HTML server controls: These are classes that wrap the standard HTML tags and are declared with the runat="server" attribute. Apart from this attribute, the declaration for an HTML server control remains the same. Two examples include HtmlAnchor (for the <a> tag) and HtmlSelect (for the <select> tag). However, you can turn any HTML tag into a server control. If there isn’t a direct corresponding class, ASP.NET will simply use the HtmlGenericControl class. To create one of these controls in Visual Studio, you need to drag an HTML element from the HTML tab of the Toolbox. Then, right-click the element, and choose Run As Server Control to add the runat="server" attribute.

Web controls: These classes duplicate the functionalities of the basic HTML tags but have a more consistent and meaningful set of properties and methods that make it easier for the developer to declare and access them. Some examples are the HyperLink, ListBox, and Button controls. In addition, several other types of ASP.NET controls (such as rich controls and validation controls) are commonly considered to be special types of web controls. In Visual Studio, you’ll find the basic web forms controls in the Standard tab of the Toolbox.

Rich controls: These advanced controls have the ability to generate a large amount of HTML markup and even client-side JavaScript to create the interface. Examples include the Calendar, AdRotator, and TreeView controls. In Visual Studio, rich controls are also found in the Standard tab of the Toolbox.

Validation controls: This set of controls allows you to easily validate an associated input control against several standard or user-defined rules. For example, you can specify that the input can’t be empty, that it must be a number, that it must be greater than a certain value, and so on. If validation fails, you can prevent page processing or allow these controls to show inline error messages in the page. In Visual Studio, these controls are found in the Validation tab of the Toolbox.

Additionally, you’ll examine several more specialized control groupings in other chapters. These include the following:

Data controls: These controls include sophisticated grids and lists that are designed to display large amounts of data, with support for advanced features such as templating, editing, sorting, and pagination. This set also includes the data source controls that allow you to bind to different data sources declaratively, without writing extra code. You’ll learn about the data controls in Chapter 9 and Chapter 10.

Navigation controls: These controls are designed to display site maps and allow the user to navigate from one page to another. You’ll learn about the navigation controls in Chapter 16.

Login controls: These controls support forms authentication, an ASP.NET model for authenticating users against a database and tracking their status. Rather than writing your own interfaces to work with forms authentication, you can use these controls to get prebuilt, customizable login pages, password recovery, and user creation wizards. You’ll learn about the login controls in Chapter 21.

C H A P T E R 4 S E R V E R C O N T R O L S

105

Web parts controls: This set of controls supports WebParts, an ASP.NET model for building componentized, highly configurable web portals. You’ll learn about WebParts in Chapter 31.

ASP.NET mobile controls: This is a set of controls that resembles the web controls but is customized to support mobile clients such as PDAs, smart phones, and so on, by rendering pages to markup standards such as HTML 3.2 or WML 1.1. The mobile controls are highly adaptive, which means that when you create a page using these controls, the page can be rendered in several completely different ways depending on the device that’s accessing the page. (This concept is also used in ordinary web controls on a lesser scale. They can generate XHTML or HTML 4.01 with JavaScript code or generate plain HTML 3.2 code according to the client browser’s capabilities.)

ASP.NET mobile controls aren’t covered in this book, although you can learn more from Derek Ferguson’s Mobile .NET (Apress, 2001).

The Server Control Hierarchy

All server controls derive from the base Control class in the System.Web.UI namespace. This is true whether you’re using HTML server controls, using web controls, or creating your own custom controls. It also applies to the Page class from which all web forms derive. Figure 4-1 illustrates the main branches of this inheritance chain.

Figure 4-1. Server control inheritance

Because all controls derive from the base Control class, you have a basic common denominator that you can use to manipulate any control on the page, even if you don’t know the specific control type. (For example, you could use this technique to loop through all the controls on the page and hide each one by setting the Visible property to false.) Table 4-1 and Table 4-2 describe the most commonly used members of the Control class.

106 C H A P T E R 4 S E R V E R C O N T R O L S

Table 4-1. Control Class Properties

Property

Description

ClientID

Returns the identifier of the control, which is a unique name created by

 

ASP.NET at the time the page is instantiated.

Controls

Returns the collection of child controls on the page. This is the “first level” of

 

controls, some of which may be container controls that contain additional

 

child controls of their own.

EnableViewState

Returns or sets a Boolean value indicating whether the control should

 

maintain its state across postbacks of its parent page. This property is true

 

by default.

ID

Returns or sets the identifier of the control. In practice, this is the name

 

through which you can access the control from the server-side scripts or

 

the code-behind class.

Page

Returns a reference to the parent page object.

Parent

Returns a reference to the control’s parent, which can be the page or

 

another container control.

Visible

Returns or sets a Boolean value indicating whether the control should be

 

rendered. If false, the control isn’t just made invisible on the client—instead,

 

the corresponding HTML tag is not generated.

 

Table 4-2. Control Class Methods

 

 

Method

Description

DataBind()

Binds the control and all of its child controls to the specified data source or

 

expression. You’ll learn about data binding in Part 2.

FindControl()

Searches for a child control with a specific name in the current control and

 

all contained controls. If the child control is found, the method returns a

 

reference of the general type Control. You can then cast this control to the

 

proper type.

HasControls()

Returns a Boolean value indicating whether this control has any child

 

controls. The control must be a container tag to have child controls (such

 

as a <div> tag).

Render()

Writes the HTML output for the control based on its current state. You don’t

 

call this method directly. Instead, ASP.NET calls it when the page is being

 

rendered.

 

 

HTML Server Controls

In the following sections you’ll learn about the HTML server controls, which are defined in the namespace System.Web.UI.HtmlControls. Overall, there are about 20 distinct HTML server control classes. They’re split into separate categories based on whether they are input controls (in which case they derive from HtmlInputControl) or can contain other controls (in which case they derive from HtmlContainerControl). Figure 4-2 shows the inheritance hierarchy.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

C H A P T E R 4 S E R V E R C O N T R O L S

107

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Figure 4-2. HTML server controls

The HtmlControl Class

All the HTML server controls derive from the base class HtmlControl. Table 4-3 shows the properties that the HtmlControl class adds to the base Control class.

Table 4-3. HtmlControl Properties

Property

Description

Attributes

Allows you to access or add attributes in the control tag. You can use this

 

collection to add attributes that are not exposed by specific properties. (For

 

example, you could add the onFocus attribute to a text box and specify some

 

JavaScript code to configure what happens when the text box gets focus in

 

the page.)

Disabled

Returns or sets the control’s disabled state. If true, the control is usually rendered

 

as a “grayed-out” control and is not usable.

Style

Returns a collection of CSS attributes that are applied to the control. In the web

 

page you set this property as a semicolon-delimited list of style:value attributes.

 

In Visual Studio, you can set this information using a designer by right-clicking

 

the control and selecting Build Style.

TagName

Returns the control’s tag name, such as a, img, and so on.