Microsoft Visual C++ .NET Professional Projects - Premier Press
.pdf
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.
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.
