asp.net 揭密中的一段话,值得参考

78 篇文章 0 订阅

Introducing ASP.NET Controls

ASP.NET controls provide the dynamic and interactive portions of the user interface for your Web application. The controls render the content that the users of your Web site actually see and interact with. For example, you can use controls to create HTML form elements, interactive calendars, and rotating banner advertisements.

ASP.NET controls coexist peacefully with HTML content. Typically, you create the static areas of your Web pages with normal HTML content and create the dynamic or interactive portions with ASP.NET controls.

The best way to understand how ASP.NET controls work in an HTML page is to look at a simple Web Forms Page.

A Simple ASP.NET Page

Let's start by looking at a normal, everyday HTML page. You'll convert this page into an ASP.NET page in a moment. First, open either Web Matrix or Notepad on your computer and enter the HTML page contained in Listing 1.1. After you finish entering the HTML page, save it with the name SimpleHTML.aspx in your inetpub/wwwroot directory.

NOTE

When you create ASP.NET pages, be sure to save them in a directory that your Web server can access. You need to save your pages in the inetpub/wwwroot directory, a subdirectory of the inetpub/wwwroot directory, or within a virtual directory of your Web server.

After you save a page, do not open the page directly in your Web browser by typing the file path of the page. If your Web server is located on the same computer as your Web browser, open the page by entering the page's address in the Address Bar of your browser. For example, if the page is named simpleHTML.aspx, open the page by typing the following text into your browser's Address Bar:

http://localhost/simpleHTML.aspx

For purposes of this book, I'll assume that you are using either Web Matrix or Notepad to create your Web pages. Web Matrix is included on the CD that accompanies this book. However, there is nothing wrong with using other text editors or Web development environments such as Microsoft Visual Studio .NET, Microsoft Visual Interdev, Allaire's Homesite, or TextPad. Any program that can save a file as plain text will work fine.


TIP

When you're saving a file with Notepad, remember to pick All Types as the value in the Save As Type box in the Save As dialog box. If you don't choose this option, Notepad automatically adds the extension .txt to the name of your file. An alternative method of preventing .txt from being appended to your filename is to place the filename in quotation marks when saving the file.


Listing 1.1 SimpleHTML.aspx
<html>
<head><title>Simple HTML Page</title></head>
<body>

<form method="post" action="SimpleHTML.aspx">
<b>Username:</b>
<br><input name="username" type="text" size="30">

<p>

<b>Comments:</b>
<br><textarea name="comments" cols=60 rows=10></textarea>

<p>

<input type="submit" value="Add Comment">

</form>

</body>
</html>

The C# version of this code can be found on the CD-ROM.

The page in Listing 1.1 contains a simple HTML form that includes a text field for a username and a text area for comments. This form might be used, for example, in a simple guest book application at a Web site. You might notice that the HTML form posts back to itself (the ACTION attribute of the <form> tag submits the form data to SimpleHTML.aspx). So, when the Add Comment button is clicked, the same form appears once again.

The HTML form in Listing 1.1 doesn't contain any ASP.NET controls. Now open the page in a Web browser (Microsoft Internet Explorer or Netscape Navigator); then try entering a username and some comments and submitting the form. Notice that the data you enter into the form fields disappears every time you submit the form.

Now modify the page in Listing 1.1 so that it uses ASP.NET controls rather than the standard HTML form elements. You're going to convert the form fields into "smart" server-side form fields. Enter the page contained in Listing 1.2 and save the file with the name SimpleASPX.aspx in a directory that your Web server can access.

Listing 1.2 SimpleASPX.aspx
<html>
<head><title>Simple ASPX Page</title></head>
<body>

<form Runat="Server">
<b>Username:</b>
<br><input id="username" type="text" size="30" Runat="Server">

<p>

<b>Comments:</b>
<br><textarea id="comments" cols=60 rows=10 Runat="Server"></textarea>

<p>

<input type="submit" value="Add Comment" Runat="Server">

</form>

</body>
</html>

The C# version of this code can be found on the CD-ROM.

Notice the four modifications to this page. First, you rename the page from SimpleHTML.aspx to SimpleASPX.aspx. The particular name of the file is not important, but the .aspx extension is very important. Your Web server detects that the page is an ASP.NET page and not a normal HTML page or other type of page because of the special .aspx extension. You create all your ASP.NET pages with this special extension.

ASP Classic Note

What about files that end with the extension .asp? Classic ASP developers (users of versions of ASP before ASP.NET) named all their ASP files with the extension .asp instead of .aspx. You can continue to name your ASP files with the extension .asp, but they do not gain any of the new functionality of ASP.NET. Files with the .asp extension continue to be executed as classic ASP files.

By default, files with the extension .asp are mapped by Internet Information Server (IIS) to asp.dll, and files with the extension .aspx are mapped to _aspnet_isapi.dll. So, both classic ASP files and ASP.NET files can happily live and work together on the same Web server.


NOTE

You might want to use an extension other than .aspx for your ASP.NET pages in several situations. For example, you might have a Web site with hundreds of existing pages with names that end with the extension .htm or .html and that have already been indexed by a search engine such as AltaVista or Google. You might not want to change the extensions of these filenames because you fear that you'll lose ranking in the search engines.

You can, in fact, use any extension that you please for ASP.NET pages. See the first appendix at the back of this book, "Migrating from ASP to ASP.NET," for details on how to do this.


Second, notice that the attribute Runat="Server" is added to all the form tags. For example, instead of using

 
<input name="username" type="text" size="30">

to create the username form field, you use the following tag instead:

 
<input id="username" type="text" size="30" Runat="Server">

The Runat="Server" attribute converts these standard HTML tags into "smart" server-side HTML tags. When the SimpleASPX.aspx page is opened in a Web browser, the form tags with this attribute are executed on the Web server before any content is rendered to the Web browser. The Runat="Server" attribute converts these standard HTML tags into ASP.NET controls.

Third, notice that instead of using the NAME attribute to name the form fields, you give the form fields a unique identifier with the ID attribute. You use the ID attribute because you are no longer treating the form fields as simple HTML tags; you are converting the form fields into server-side objects.

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值