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

Microsoft Visual C++ .NET Professional Projects - Premier Press

.pdf
Скачиваний:
180
Добавлен:
24.05.2014
Размер:
26 Мб
Скачать

520

 

Project 4

CREATING A MANAGED EXTENSIONS CLASS LIBRARY

 

 

 

 

 

 

 

 

 

 

Table 15-2 Options for Executing an ASPX Page

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Option

Use

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

View in Browser

You can choose this option to open the page in the built-in

 

 

 

 

 

 

browser of Visual Studio .NET.

 

 

 

 

Browse With

You can choose this option to open the page with Internet

 

 

 

 

 

 

Explorer or with the default built-in browser of Visual Studio

 

 

 

 

 

 

.NET.

 

Y

 

 

 

 

Build and Browse

 

 

L

 

 

 

 

You can choose this option to build and then display the page in

 

 

 

 

 

 

the default built-in browser of Visual Studio .NET.

 

 

 

IIS

Application Root DirectoryF

 

 

 

 

 

When you create an ASP application, you specify the path of the project as either

 

 

 

 

 

 

 

M

 

 

 

 

 

 

http://localhost or http://computer name. However, since these paths do not

 

 

 

 

specify the path of the directory,Ayou might find it difficult to trace the applica-

 

 

 

 

tion. ASP.NET has a solution for this problem. Now every time you create an

 

 

 

 

 

 

E

 

 

 

 

 

 

 

ASP.NET Web Application project using ASP.NET, the application root direc-

 

 

 

 

 

 

T

 

 

 

 

 

 

 

 

tory is created by default at a fixed location. The directory is created in the default

 

 

 

 

Web site of the IIS server (http://localhost or http://computer_name) with the

same name as the name of the ASP.NET Web Application Project. The application root directory maps to the path c:\Inetpub\wwwroot\project_name.

You can trace the application root directory by choosing Start, Programs, Administrative Tools Internet Services Manager. This will open the Microsoft Management Console. In the Default Web Site directory, you can find the application root directory for your application.

When you create an application, there are three folders that are created. You can differentiate the folders by their icons:

Explorer-style folders. These are the folders that are physically located in IIS. For example, the _vti_pvt and _vti_txt folders.

Explorer-style folders with small globes. These type of folders represent the virtual directories. For example, the _vti_bin folder.

Web application folders. These types of folders appear as package icons. When you create a virtual directory, by default, this directory is treated as a Web application. For example, when you create an application

Team-Fly®

INTRODUCTION TO ASP.NET

Chapter 15

521

 

 

 

 

named myapplication by using ASP.NET, a folder named myapplication is created automatically in IIS.

The application is created in the root directory of IIS only when you use Visual Studio .NET applications to create ASP.NET applications. However, if you are using a text editor, you will need to create the application root directory manually. You can create an application root directory in either of two ways:

By creating a virtual directory

By marking a folder as an application

Creating a Virtual Directory

As mentioned in the previous section, you can create an application root directory manually by creating a virtual directory. You create a virtual root directory to publish the page so that the page can be deployed on a Web site. The following are the features of a virtual root directory that make it suitable for creating an application root directory:

A virtual directory does not need to be located in the physical structure of the root directory of the Web server. The location could be completely different or even could be on a remote computer.

Different virtual directories can point to the same set of files.

When you create a virtual directory and point it to your application that is stored in the root directory of the Web server, the virtual directory becomes the application root directory for your application.

You can create a virtual directory by following these steps:

1.Open the IIS MMC from the Internet Service Manager. Then, rightclick on Default Web Site.

2.Choose New, Virtual Directory from the context menu. The Virtual Directory Creation Wizard appears.

3.Click on Next on the Welcome screen to proceed. You are prompted to enter an alias for the virtual directory.

4.Type an alias name for your virtual directory and click on Next to move to the next screen.

5.Type the name of your virtual directory. Click on Next. The name will appear in the IIS MMC. The next screen prompts you to enter the name of the content directory that you want to publish on the Web site.

522Project 4 CREATING A MANAGED EXTENSIONS CLASS LIBRARY

6.In the Directory box, type the name of the directory (the complete path) that contains your ASP.NET Web Forms page and other related files, and then click on Next. The next screen allows you to set the access permissions for the virtual directory.

7.Select the access permissions and click on Next. Here you can assign permissions, such as Read and Run scripts for the virtual directory.

8.Click on Finish to complete the process.

Marking a Folder as an Application

In the previous section, you learned about creating an application root directory by creating a virtual directory. You can also create an application root directory by marking the folder that contains the ASP.NET files as an application. However, before you mark a folder as an application, you need to create a folder in the C:\Inetpub\wwwroot directory. The folder will then be visible when you launch the IIS MMC under Default Web Site.

TIP

If the IIS MMC is already launched, select Default Web Site and then click on the Refresh button on the toolbar. This will activate the latest changes.

When the folder is ready, you can mark the folder as an application. To do so, rightclick on the folder and choose Properties to open the Properties dialog box. Notice that the Properties dialog box displays the local path and permissions for the folder that you select. To convert this folder into an application, follow these steps:

1.Under Application Settings, click on the Create button.

2.Click on OK to complete the process. Notice that after you convert the folder into an application, the icon of the folder changes to a package icon, indicating that the folder was successfully converted into an application.

The ASP.NET Web Forms Server Controls

There are millions of Web sites on the Web today. For your Web site to be noticed by Web surfers, you need to ensure that it is well designed and structured. ASP.NET provides several controls that you can use to create dynamic and

INTRODUCTION TO ASP.NET

Chapter 15

523

 

 

 

 

interactive Web Forms pages. You can use the controls to interact with the server. In the next section, you’ll learn about the types of server controls available in the

.NET Framework.

There are two types of server controls that the .NET Framework supports: HTML server controls and Web server controls. However, when creating ASP.NET applications, it is advisable to use Web server controls, because you cannot modify the properties of an HTML form after the page has been rendered on the Web. In contrast, on an ASP.NET page that uses Web server controls, you can access properties, methods, or events on the server side.

In addition, there are validation and user controls that you can add to your Web pages. Validations controls are used to validate the values entered by the user in other controls located on the Web page. User controls are created as other Web Forms pages and can be used in other Web Forms pages. You can use this functionality of the user controls to create user-interface applications, such as toolbars and menu options.

HTML Server Controls vs. Web Controls

As mentioned before, while creating ASP.NET applications, developers can opt to use either HTML server controls or Web controls. However, before making the decision, it is important to decide which of the two server controls is best suited for your requirements. Table 15-3, which lists the differences between the two types of controls, will help you to decide.

Table 15-3 Comparing HTML Server and Web Controls

Comparison Factor

HTML Server Controls

Web Controls

Mapping to HTML tags

Map directly to HTML tags.

 

Developers can use the runat=

 

“server” attribute to convert the

 

HTML tags to server controls.

 

This eases the migration process

 

from ASP to ASP.NET.

Do not map directly to HTML tags.

Object model

Based on the HTML-centric object

 

model. Therefore, each control

 

has a set of attributes.

Based on the Visual C#- like programming model. Therefore, each Web control has a set of standard properties.

continues

524 Project 4 CREATING A MANAGED EXTENSIONS CLASS LIBRARY

Table 15-3 (continued)

Comparison Factor HTML Server Controls Web Controls

Target browser

The HTML code that is generated

 

remains the same irrespective of the

 

target browser. Therefore, it’s the

 

developer’s duty to ensure that the

 

controls are rendered in both up-

 

level and down-level browsers.

The rendered output depends on the target browser and is automatically adjusted according to the browser. Therefore, the developer need not worry, because the controls are automatically rendered in both uplevel and down-level browsers.

Adding Server Controls to a Form

You can add server controls to Web Forms either at run time or at design time. To add server tools at design time, you can either use the Toolbox or HTML view of the ASPX file. You add server controls at run time by using the <SCRIPT> tag either in the ASPX file or the code-behind file. The following section talks about adding server controls to the form by using the Toolbox.

The Toolbox

The Toolbox in Visual Studio .NET includes a variety of tools that you can use on your Web pages. It is located on the extreme left corner of the Visual Studio

.NET window. You can also launch the Toolbox from the menu by choosing View, Toolbox.

The controls in the Toolbox are grouped under special categories for the convenience of the developers:

Web Forms. Contains Web controls that you can use on Web Forms page. The controls include rich Web controls and validation controls.

HTML. Contains HTML server controls.

Data. Contains the data objects that are used to implement data access and manipulation functionality.

Component. Contains components that are used to add time-based functionality to forms.

INTRODUCTION TO ASP.NET

Chapter 15

525

 

 

 

 

Clipboard Ring. Contains pieces of text. Text that you cut or copy becomes a part of this category.

General. Contains the Pointer tool. You can choose to add controls such as Buttons and custom controls to this category.

You can insert the Web Forms controls by dragging the control from the Toolbox to the Web Forms page. An alternative way to add a control is by double-clicking on the control and then specifying the coordinates of the location on which you want to place it on the form. Note that when you drag a control to the Web Forms page, the code for the control is by default added in the ASPX file.

Using the HTML View of the ASPX File

You can also add server controls programmatically by writing the required ASP.NET code in HTML view of the ASPX file of your Web form. Consider an example where you add a text box by using ASP.NET code:

<asp:TextBox id = “MyTextBox” runat = “server” Text = “Greetings”></asp:TextBox>

To view the output of the preceding code, you need to switch to Design view.

Using the Code-Behind File

In the previous section, you learned about adding controls at design time. However, ASP.NET gives you the power and flexibility of creating a control at run time. You can do so by creating an instance of the control class that inherits the WebControl base class. Consider the following code that creates a text box by using Visual C# code:

System.Web.UI.WebControls.TextBox Text_Box=new TextBox();

this.Controls.Add(Text_Box);

In the preceding code, a TextBox control is added by using the Add method of the

ControlCollection class.

Setting Properties for Web Controls

Once you have added a server control to a Web Forms page, you can define the control’s properties. All server controls have common properties, referred to as base properties, which are inherited from the WebControl base class. In addition to the base properties, each control also possesses its own set of properties. You have the option of specifying these properties either at design time or at run time.

526 Project 4 CREATING A MANAGED EXTENSIONS CLASS LIBRARY

Setting Properties at Design Time

You can set properties for a control at design time in either of two ways:

Using the Properties window. You can access the properties for a control by opening the Properties window. To open the Properties window for the control, select the control and then select Properties from the pop-up menu. You can also open the window by choosing Properties window from the View menu or by pressing F5.

Figure 15-4 displays the Properties window for a TextBox control.

FIGURE 15-4 Properties window for a TextBox control

Writing ASP.NET code. You can also add a control to the Web Forms page by writing the code for specifying the properties of the control. For example, to set the Enabled property of a TextBox control in the ASPX file, you can use the following code:

<asp:TextBox Id = “Text_Box” runat = “server” Enabled =

False></asp:TextBox>

Setting Properties at Run Time

You might want a control to be displayed only when a certain event occurs or a condition returns true. For example, you might want to display a control only when the user selects the right answer. In such a situation, you can set properties for a control at run time. However, to do so, you need to write the code for chang-

INTRODUCTION TO ASP.NET

Chapter 15

527

 

 

 

 

ing the property at run time. The following code demonstrates how you can programmatically change the name of a control at run time:

ControlID.PropertyName = Value

Now, consider the following code to understand better how the syntax can be used:

Text_Box.Enabled = true

In the preceding code:

The Enabled property is the property of the control.

The ControlID property is specified as Text_Box.

The Enabled property is set to True.

Now that you are familiar with changing the properties of a control, the next section explains how you can handle the different events raised by a control.

Handling Events of Web Controls

Although all controls inherit a certain set of events from the WebControl class, they also have a certain set of events that is unique for that control. Table 15-4 lists the commonly used properties, methods, and events common to all Web controls.

Table 15-4 Properties, Methods, and Events of the WebControl Class

Property/Method/Event

Function

AccessKey property

Used to get or set the keyboard shortcut to access the

 

control.

BackColor property

Used to get or set the background color of the control.

BorderColor property

Used to get or set the border color of the control.

BorderStyle property

Used to set the border style of the control. The border can

 

be set as double, solid, or dotted.

BorderWidth property

Used to get or set the border width of the control.

Controls property

Used to return an object of the ControlCollection class.

continues

528 Project 4 CREATING A MANAGED EXTENSIONS CLASS LIBRARY

Table 15-4 (continued)

Property/Method/Event

Function

DataBind method

Used to bind data from a data source to a server control

 

and all its child controls.

DataBinding event

Generated when the control is bound to a data source.

Enabled property

Accepts a Boolean value indicating whether the control is

 

enabled.

EnableViewState property

Accepts a Boolean value indicating whether the control

 

maintains its viewstate.

Font property

Used to get or set the font information. The font informa-

 

tion could be related to the font name and font size of the

 

control.

ForeColor property

Used to get or set the foreground color of the control.

Height property

Used to get or set the height of the control.

ID property

Used to get or set the identifier of the control.

TabIndex property

Used to get or set the tab index of the control.

ToolTip property

Used to get or set the pop-up text displayed when the

 

mouse pointer is moved over the control.

Visible property

Accepts a Boolean value. True indicates that the control is

 

rendered as part of the UI on the page. False indicates that

 

the control isn’t rendered on the page.

Width property

Used to get or set the width of the control.

 

 

You can handle the events by either using the code-behind file or the ASPX file. To better understand this concept, consider the example of a Button control. A Button control possesses its own Click event. There are two ways that you can handle this event: use the code-behind file or use the ASPX file.

The steps to handle the Click event of the Button control using the code-behind file are as follows:

1.Open the code-behind file by either using the Solution Explorer or pressing the F7 key.

INTRODUCTION TO ASP.NET

Chapter 15

529

 

 

 

 

2.Write the handler for the control’s event. To do so, select the name of the control from the Class Name list. Next, select the event from the Method Name list.

Consider the following example:

private void AcceptButton_Click(object sender, System.EventArgs e)

{

}

In the preceding code:

The ID of the button is specified as AcceptButton.

AcceptButton_Click is the event handler for the Click event of the button with the ID AcceptButton.

The procedure accepts two arguments.

The first argument contains an object called event sender.

The second argument contains the data for the event.

The Handles keyword is used to associate the Click event with the event

handler; i.e., AcceptButton_Click.

The next step is to modify the code in the event handler to add the desired tasks. You can also create event handlers for server controls by using the ASPX file. Consider the preceding example of a Button control with ID AcceptButton. The creation and association of the event handler with the Click event is done in two steps:

1.Edit the code for the control in the ASPX file. This is used to specify the event handler for the Click event:

<asp:Button Id = “AcceptButton” runat = “server” OnClick =

“AcceptButton_Click”></asp:Button>

In this code snippet, OnClick = “AcceptButton_Click” is the event that should be called when the Click event of the button is generated.

2.Write the AcceptButton_Click event handler in the ASPX file. Recall that all the code is written within the <Script> tag.

The following code shows where to add an event handler:

<script language=”cs” runat=”server”>

private void AcceptButton_Click(object sender, System.EventArgs e)