Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
CSharp - Your Visual Blueprint For Building .NET Applications (2002) [eng].pdf
Скачиваний:
32
Добавлен:
16.08.2013
Размер:
9.71 Mб
Скачать

C#

CREATE AN ASP.NET WEB SITE

SP.NET applications can run on the same platform as AASP applications. ASP.NET applications are supported

on the IIS Web server. ASP.NET pages require preprocessing by the aspnet_iaspi.dll.

Similar to creating an ASP site, when you create a Web site for ASP.NET, you need a virtual directory configured as an application. ASP.NET applications are supported on Windows 2000 and Windows NT 4 with Service Pack 6a, with the exception of using Web Services. Web Services are supported on all platforms supported by the Microsoft .NET Framework SDK, except Windows 95. The Microsoft .NET Framework SDK is supported on Windows 2000,

Windows NT 4 with Service Pack 6a, Windows Me, Windows 98, Windows 98 SE, and Windows 95.

All ASP.NET applications that you configure in a virtual directory have a special icon that is assigned in the IIS Microsoft Management Console, or MMC. This icon is different than the standard icon for a virtual directory that is not an application or just a subdirectory of the root Web site. An icon that is configured as a virtual directory looks like an open package versus a standard folder icon that you see in Windows Explorer. You can go into the IIS MMC to configure the site or just let VS .NET take care of this for you.

CREATE AN ASP.NET WEB SITE

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

ˇ Type a name for your

Click File New

The New Project window

.

appears.

Web application.

 

Click Visual C# Projects

 

Á Click to select

 

 

 

for the Project Type.

http://localhost for

 

Click the Empty Web

your location.

 

 

Click OK.

 

Project icon for the Templates

 

 

 

pane.

PROGRAMMING WEB APPLICATIONS 11

You can easily trace the execution of code in ASP.NET by placing the Trace attribute in the @Page directive. If you desire to trace the entire application, you can change the web.config file. You search for the trace tag in the web.config file. Make sure both enabled and pageOutput attributes are set to true. The output tracing gives details on the page request, execution time for page process, control sequence, cookie information, headers, and server variables.

TYPE THIS:

<%@ Page Trace="true" %>

RESULT:

The Request Details, Trace Information, Cookies Collection, Headers Collection, and Server Variables are displayed at the bottom of your aspx page in the form of tables.

Solution Explorer - VisualCSharpBlue...

VisualCSharpBlueprint

The Create New Web dialog box indicates that the new Web application is being created.

The project is created on http://localhost. You can use this project to create a Web site.

219

C#

CREATE A WEB FORM

he majority of your ASP.NET application consists of TWeb Forms and their corresponding code-behind files.

Web Forms give you the flexibility of separating code from presentation, which promotes better structured code that is easier to develop and maintain.

To create a Web Form, you add an .aspx page to an existing site. See page 218 for details on creating a new Web site. When you implement server-side code for the

.aspx page, you create an aspx.cs page to house the code-behind page. The extension of this file ends with .cs, which indicates that you programmed the code in the code-behind page in C#. If you implemented the page with

Visual Basic, the extension is aspx.vb. Note that the aspx part of the extension is optional.

Implementing the server-side code that supports your Web page can be done either with <script> blocks in your

HTML or with code-behind pages. Using code-behind pages allows for cleaner separation of code. Either way, you will create event handlers that contain the implementation of the code necessary to make your page functional. For example, you can use the Page_Load event to initialize controls on your Web Form. This is similar to the Form_Load event that is used in VB forms development.

CREATE A WEB FORM

Solution Explorer - VisualCSharpBlue...

VisualCSharpBlueprint

. ¤ Add an ASP.NET Web

page by clicking File Add

New Item from the Visual

Studio Menu.

Click Web Project Items to select a Category.

Click Web Form to select a Template.

ˇ Type a name for the Web Form with an .aspx extension.

Á Click Open.

A Web page with a Web Form appears in Design mode.

PROGRAMMING WEB APPLICATIONS 11

When developing Web Forms, you can implement server-side code in two ways. The first implementation, well supported in VS .NET, involves creating an additional code-behind page containing an extension of .cs. The second implementation is embedding a server-side <script> tag.

Example:

<html>

<script language="C#" runat="server">

void Submit_Click(object sender, EventArgs e) { if (txtName.Value == "RobertPhillips" &

txtPwd.Value == "pharmacist") spnMessage.InnerHtml = "You are authenticated!";

else

spnMessage.InnerHtml = "Login Failed!";

}

</script>

<body> <form method=post runat=server>

<h3>Enter Name: <input id="txtName" type=text size=40 runat=server> <h3>Enter Password: <input id="txtPwd" type=password size=40 runat=server> <input type=submit value="Enter" OnServerClick="Submit_Click" runat=server> <h1><span id="spnMessage" runat=server> </span></h1>

</form></body></html>

Solution Explorer - VisualCSh...

CreateWebForm.aspx

Click View Toolbox to open the Toolbox panel.

° Click the HTML tab to display the HTML controls.

· Double-click the Label button in the Toolbox.

A label control appears.

If your Solution Explorer panel is not open, click View Solution Explorer to open it.

Right-click the filename in the Solutions Explorer window and choose Build and Browse.

The Web page appears with a label control in the Preview window.

221

Соседние файлы в предмете Программирование на C++