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

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

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

510 Project 4 CREATING A MANAGED EXTENSIONS CLASS LIBRARY

objects and interface at the front end. The real power of this demarcation is that dynamic Web pages can easily and frequently be updated, thus resulting in up-to-date content on the Web.

An all-new server processing architecture. In earlier versions of ASP, the code was executed in order of writing. This resulted in developers spending a lot of time determining where the code blocks needed to be

placed. However, this problem has now been rectified in ASP.NET. ASP.NET introduces server-based controlsYthat are created on the server side and are event driven. This impliesLthat the code that needs to be executed first is based on theFuser’s actions. This server processing architecture allows developers to focus on developing the application rather than spending a considerableMamount of time figuring out where a block of code needs to beAplaced.

New improvedEmechanism for caching. ASP.NET offers new caching features that help in effective cache maintenance. The new caching features are pageT-level caching, which allows a developer to cache an entire Web page, fragment caching, which allows a developer to cache selected portions of a Web page, and the Cache API, which allows a developer to cache objects that they have created.

Better support for debugging and tracking. ASP.NET allows developers to use built-in debugging tools available with Visual Studio .NET to debug their code. In addition, Visual Studio .NET includes methods for tracing Web pages.

Better application configuration options. Apart from the standard IIS settings that are common for both ASP and ASP.NET, ASP.NET helps you configure application-level information. This information is stored in XML format and can be accessed whenever required. As a result, you need not tamper with the registry settings to modify your configuration settings.

Ease of deployment. ASP.NET makes deploying Web applications an easy task. All you have to do is copy the directory to the document root of your Web server. What’s more, you don’t need to restart the server every time you deploy or replace the code. In addition, when you recompile the code, all new requests are automatically redirected to the replaced code.

Team-Fly®

INTRODUCTION TO ASP.NET

Chapter 15

511

 

 

 

 

Requirements for Creating an ASP Application

ASP.NET is a part of the .NET Framework SDK (Software Development Kit.) You can download the .NET Framework SDK from the Microsoft site www.msdn.microsoft.com/downloads. However, for the proper functioning of the .NET Framework, you also need to install Internet Explorer 5.5 or later on your computer. In addition, the .NET Framework can be used and configured on several Windows operating systems, such as:

Windows NT 4.0 (with Service Pack 6 installed)

Windows 2000

Windows XP Professional

Windows Me

Windows 98

Note that though ASP.NET applications can be created on Windows 98, Windows Me, or Windows XP Professional platforms, to work with ASP.NET on these platforms, you need to install a Web server (preferably IIS) before beginning development. However, if you are using Windows 2000 Server or Windows NT 4.0, IIS is installed automatically.

After you have installed the .NET Framework SDK, you need to install a text editor or a command-line compiler, because although the .NET Framework SDK provides several tools that can be used to test as well as develop ASP applications, it does not provide the development environment. To develop ASP.NET applications, you require text editors and command-line compilers. The following text editors and compilers are installed when you install Visual Studio .NET:

Smart code editors. These are intelligent text editors that have features such as statement completion and syntax checking.

Visual Editors. These allow you to simply drag and drop controls instead of exclusively writing code for each control.

Built-in compilers and debuggers. These help avoid compiling applications using the command prompt.

512 Project 4 CREATING A MANAGED EXTENSIONS CLASS LIBRARY

ASP.NET Architecture

You learned about the different subsystems that you can use to create, build, and deploy ASP.NET applications. To create an ASP.NET application, you need to correlate the different subsystems. However, before you do that, you need to have a basic understanding of the ASP.NET architecture.

As Figure 15-1 indicates, users gain access to ASP.NET applications using the Web server (IIS).

Web clients

IIS

 

 

Operating system

ASP.NET application

 

 

Windows NT/2000

 

 

 

 

 

 

 

 

.NET Framework

FIGURE 15-1 The ASP.NET architecture

There are two programming models available with ASP.NET that can be used to create Web applications:

Web Forms. This programming model allows you to create form-based, dynamic Web pages with ease. The server control helps developers create user-friendly UI components. You can later use server-side programming to make these controls work in the desired fashion.

INTRODUCTION TO ASP.NET

Chapter 15

513

 

 

 

 

Web services. This programming model is the best possible choice for integrating applications across various platforms. Web services are also used to exchange information in a client/server or server/server architecture. Web services use HTTP and XML messaging for data exchange over the Internet.

Web Forms Basics

Web developers can use Web Forms pages to create programmable Web pages that present an interface for your ASP application. The Web Forms pages in ASP.NET are created using the Web Forms programming model, which offers several features that make creating Web Forms pages an easier and more pleasurable experience:

CLR features such as inheritance and type safety.

A rich set of controls from which the developer can choose to easily create a user-friendly interface.

The programming model makes it possible to extend Web Form functionality programmatically.

Web Forms pages are the Web pages that you create using the ASP.NET programming model. As previously mentioned, you can use Web Forms pages to create an interface for your Web applications. The features of Web Forms pages can be summed up as follows:

Web Form pages are designed by using rapid application development (RAD) tools that are part of Visual Studio .NET.

Web Forms pages are not dependent on the client. This implies that the client might be using any platform, the Web page will still look and function in the same way.

Web Forms pages are compatible with most Web browsers and mobile devices.

Components of Web Forms

As mentioned in the previous section, one of the most powerful additions to ASP.NET is a clear demarcation between content and application logic. A Web Forms page is built using the same feature. A Web Forms page is divided into two pieces — the visual component and the logic portion. The visual component, or

514 Project 4 CREATING A MANAGED EXTENSIONS CLASS LIBRARY

UI, is used to present content to the client. The UI consists of a file that contains HTML or ASP.NET controls. The logic portion of the Web Forms page contains the code for the form. The programming logic can be written in the ASPX file without mixing it with the HTML code. This model of programming is referred to as the code inline model. The programming logic can also be written in a file other than the ASPX file. This model of programming is referred to as the codebehind model, and the file that stores the programming logic is called the codebehind file.

Creating Web Forms

In this section, you’ll create a simple Web Forms application. To create an application using ASP.NET’s Web Forms application, follow these steps:

1.Open the New Project dialog box by choosing File, New, Project.

2.In the Project Types pane, select Visual C# Projects.

3.In the Templates pane, select ASP.NET Web Application.

4.The default name of the project, WebApplication1, appears in the Name box.

5.You also need to specify the name of the computer on which IIS is installed. Type the name of the computer in the Location box. The format for this is http://<name of the computer>. The default location is http://localhost.

6.Click OK.

Figure 15-2 displays how the Visual Studio .NET window looks like after you have performed the preceding steps.

Figure 15-2 displays the Web Form. This Web Form is, by default, named WebForm1.aspx. To rename the Web Form, right-click the Web Form in Solution Explorer and choose Rename from the pop-up menu.

As shown in Figure 15-2, there are multiple files under WebApplication1 in the Solution Explorer window. Table 15-1 describes these files.

INTRODUCTION TO ASP.NET

Chapter 15

515

 

 

 

 

FIGURE 15-2 The Visual Studio .NET window

Table 15-1 Files Created by the Web Application Wizard

File

Content

Web.config

Application configuration information

Global.asax

Application-level event handlers

Licenses.lics

The license information

YourFirstApplication.vsdisco

Informational links to ASP.NET Web services that will

 

be used in your application

AssemblyInfo.cs

Information, such as versioning and dependencies, for

 

your assembly

Styles.css

A description of default HTML style settings

 

 

In addition to the files listed in the preceding table, the application adds references to the namespaces, which are stored in the References folder. Another folder, named bin, is also created. This folder contains the DLL files used in the application. To view the bin folder, you need to click the Show All Files icon in the Solution Explorer window.

516 Project 4 CREATING A MANAGED EXTENSIONS CLASS LIBRARY

After you have added the form to the project, you need to drag controls from the Toolbox. By default the Grid Layout is the active layout. The Grid Layout helps developers place the controls accurately on the form.

Alternatively, you can also use the Flow layout to design the page. This layout helps developers directly insert text in the Web page. You can change the layout of the Web page in accordance with your preferences. For example, you can change the layout to Flow layout by first selecting the page in Design view and then pressing the F4 key. This activates the Properties window for that page. In this window, you can select the FlowLayout in the pageLayout property.

When you drag controls to the form in Design view, Visual Studio .NET by default adds the corresponding HTML code for the controls. You can use HTML view to view and modify the code. However, you should try modifying the code only if you are fairly comfortable with the software. You can switch between HTML view and Design view by clicking on the HTML and Design tabs at the bottom of the page (as shown in Figure 15-3.)

Apart from the page file, each Web Form that you create also contains a corresponding code-behind file. The code-behind file contains the coding done using languages such as Visual C#. You can view the code-behind file by pressing the F7 key when the page file is open or by double-clicking the page in Design view. To view the code-behind file, click on the Show All Files option in the Solution Explorer window.

Now that you have the basic structure of the Web form in place, you need to add functionality to it. To add functionality, you can use HTML view of either the

FIGURE 15-3 The Web Form designer

INTRODUCTION TO ASP.NET

Chapter 15

517

 

 

 

 

page file or the code-behind file. But before you begin editing the code present in these files, you should understand their existing code. When you view the page in HTML view, the following code appears:

<%@ Page Language=”cs” AutoEventWireup=”false” codebehind=”MyForm1.aspx.cs”

Inherits=”YourFirstApplication.MyForm1”%>

In the preceding code:

The @Page directive is used to specify the page attributes and how these page attributes will affect the structure of your page.

The Language attribute is used to specify the supported .NET language for your page.

The AutoEventWireup attribute is used to specify whether the page events are automatically wired.

The codebehind attribute is used to specify the name of the code-behind file for your page.

The Inherits attribute is used to specify the name of the code-behind class that a page inherits

You can specify the logic for your code in the <HEAD> attribute of the HTML code. To do so, you will need to use the <SCRIPT> tag. Consider the following code:

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

code here

</Script>

In the preceding code:

The <SCRIPT> tag is used to add the programming logic.

The code statement runat=”server” is used to specify that the code will be executed on the server side.

The statement language=”cs” is used to specify that C# has been used to add the programming logic. The language that you specify in this attribute should be supported by the .NET Framework.

Next, you need to add the controls or the text to the <BODY> tag. You also need to generate the code for the code-behind file. The following is the code that appears in the MyForm1.aspx.cs file:

518 Project 4 CREATING A MANAGED EXTENSIONS CLASS LIBRARY

public class WebForm1 : System.Web.UI.Page

{

protected System.Web.UI.WebControls.Button Button1; private void Page_Load(object sender, System.EventArgs e)

{

}

#region Web Form Designer generated code override protected void OnInit(EventArgs e)

{

//

// CODEGEN: This call is required by the ASP.NET Web Form Designer.

//

InitializeComponent();

base.OnInit(e);

}

///<summary>

///Required method for Designer support - do not modify

///the contents of this method with the code editor.

///</summary>

private void InitializeComponent()

{

}

#endregion

}

}

In the preceding code:

The first line suggests that the class named MyForm1 is inherited from the Page class.

This Page class is a part of the System.Web.UI namespace.

This code also contains two methods, InitializeComponent and

Page_Init.

InitializeComponent contains the code that initializes page components such as controls.

Page_Init is the event handler for the Init event of the page. This method is used to call the InitializeComponent method.

INTRODUCTION TO ASP.NET

Chapter 15

519

 

 

 

 

You can then use the Page_Load method to specify the code for tasks that need to be performed immediately after the form is loaded in the browser. Additionally, you can add event handlers for each of the controls that you have added on your page.

You will now implement the knowledge that you have gained so far by creating an application that displays a welcome message. The steps to create the application are as follows:

1.Open the Properties window for MyForm1 page.

2.Set the bgcolor property of the form, MyForm1, to #23fff.

3.Open the code-behind file for your application. In lieu to this example open the MyForm1.aspx.cs file. You will use this file to modify the existing code.

4.Write the following code in the Page_Load method:

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

{

String UserName;

UserName = Request.QueryString[“Name”];

Response.Write(“<Center>” + “<B>Welcome</B>” + “<B> “ + UserName + “</B>” + “</Center>” + “<br><br>” + “<Center>” + “</Center>”);

}

To run the application, press Ctrl+F5. A welcome message is displayed when the user specifies a name.

An ASP.NET application contains several Web forms. While creating the application, you should test the functionality for a single page instead of testing the functionality of the entire application in one go. To test the functionality for each page, right-click on the ASPX page. From the pop-up menu that appears, choose the option that you want. Table 15-2 lists the options available in this menu.

Now that you have created a form, you can publish it on a server. The following section describes the directory structure that you need to follow to publish your application on the IIS server.