转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 站长学院 >> Web开发 >> 正文
Hello World:解剖 ASP.NET 项目         ★★★★

Hello World:解剖 ASP.NET 项目

作者:闵涛 文章来源:闵涛的学习笔记 点击数:2035 更新时间:2009/4/23 10:48:40

Hello World: An Anatomy of an ASP .NET Project

by Kaushal Sanghavi

Introduction

Ever since Kernighan and Ritchie wrote a program to display "Hello World" in the C language, programming has never been the same. I have learnt and programmed a variety of new languages since then, and my first attempt at each new programming language has been to greet the world with a "Hello World". ASP .NET is not just a new revision of ASP. It is not ASP 4.0. It is a completely new paradigm and a new programming model and language. What better way to be introduced to this language than with a Hello World?

In this article, I will attempt to write a simple "Hello World" and explain all the code that goes behind this. ASP .NET generates a lot of code and files for you to support the notion of a Web Application, and we will dig into the details of an ASP .NET project.

A note about the attached code is due here. Due to the multiple files that VS .NET creates, the references to drives, and folders, it turned out to be very difficult for me to zip up all the code and link it to this document. It would have been next to impossible for someone to download this code and get it working "out of the box". As a result, I am only attaching the HelloWorld.aspx and HelloWorld.vb files and I would recommend you create an empty ASP .NET project and add these files to your project. A zipped version of the source code is available for download from here.

How to Create an ASP .NET Project

Creating an ASP .NET project is fairly simple. You will need to have the Beta 1 version of Microsoft Visual Studio .NET. In reality, you don''''t need this tool, it is possible to open up your favorite editor and start typing, but VS .NET provides a whole lot more than your simple Notepad. I will focus on creating ASP .NET projects using Microsoft VS .NET Beta 1. In order to create an ASP .NET project, follow these simple steps.

  1. Chose "New Project" from VS .NET IDE.
  2. Under the "Project Type", select "Visual Basic Projects" and chose the "Web Application Template".
  3. Enter the name of your ASP .NET project (ASPHelloWorld in this case) and voila! You have just created your first ASP .NET project.
  4. Rename the WebForm1.aspx to a more meaningful name. In this sample, I have renamed it to HelloWorld.aspx and this is how I will refer to it in this article.

Files generated by VS .NET

When you create an ASP .NET project, VS .NET by default creates the following main files for you. You should be able to view and open these files using the Solution Explorer. Let me start with a list of these files and a quick description of this. In the next section, I will deep deeper into each file and try to explain each in more detail.

ASPHelloWorld.disco
Used for dynamic discovery of Web Services
Config.web
Contains the configuration settings for this Web Application
Global.asax
Similar to the Global.asa file. Contains global events for this Web Application
Global.vb
This contains the event handling code corresponding to the Global.asax
HelloWorld.aspx (by default, this file is called WebForm1.aspx)
Blank by default. This is where your ASP\HTML controls go in.
HelloWorld.vb
This contains the event handling code for the corresponding .aspx file
Styles.css
Style sheet that can be used across the application

In addition to these, VS .NET also creates the following files that are used internally by VS .NET. These contain details about the project settings etc. I would recommend you to open these in notepad and view them in order to understand how VS .NET uses these, but be careful not to modify these. Modifying these files could cause VS .NET to not recognize your project and not be able to open the solution.

ASPHelloWorld.sln
ASPHelloWorld.suo
ASPHelloWorld.vbproj
ASPHelloWorld.vbproj.webinfo

Digging into Code

Now that we have covered the basics, its time to dig into the real code.

Please enter your name

This is what the HelloWorld webform looks like. It asks you for your name and then proceeds to greet the world "Hello World" followed by your name in parenthesis.

Hello World (by Kaushal)

Now that we know what the code does, lets dive into the code.

HelloWorld.aspx Details

Lets start with digging into the details of the .aspx file that has been generated. Keep in mind, I have not written a single line of HTML for this. This is a very VB-sque experience. VS .NET provides a standard Toolbox that contains WebForm controls, HTML controls, Server controls etc, and building the UI is as simple as dragging and dropping these controls and setting their properties in the Properties window (again a feature inspired by the VB 6.0 Properties window). I will take certain sections of the generated code and discuss this. If you want to see how these sections fit together, or would like to look at the code in its entirety, I would suggest that you open the code (from the zip file) in VS .NET and follow along.

Page Level Attributes

The first thing you will notice when you open up the HelloWorld.aspx in an editor is this line:


<%@ Page Language="vb" Codebehind="HelloWorld.vb" Inherits="ASPHelloWorld.HelloWorld"%>

This statement describes certain page level attributes for this aspx file. ASP .NET introduces a new syntactical construct <%@ ... %> which is used for specifying attributes. Every aspx file will have a statement similar to this that describes attributes such as the class that contains the event handler code, the name of the file containing the code, the language etc. This is necessary because ASP .NET cleanly distinguishes the presentation from the code. The aspx file only contains the HTML tags and server controls, while all the code needed to process the events that are fired by these controls are contained in a separate file. This allows the separation of effort between graphic designers who could be building beautiful pages and software engineers who could be writing beautiful code. The current ASP model mixes the HTML tags and controls with code, and anyone trying to read, maintain, or debug a 5000 line ASP file can attest to this.

There are too many ASP .NET attributes that can be covered here, so I will concentrate on some of the most important ones.

  • Language: This tells the ASP .NET compiler (yes! ASP .NET is compiled) that the language used is VB. Currently ASP .NET supports VB, JavaScript and C#.
  • CodeBehind: Determines the file that contains the event handling code for this aspx file.
  • Inherits: Determines the class that will contain methods to handle events on this page.
  • AspCompat: Determines whether this page is backward compatible with ASP.
  • Buffer: Determines whether page level buffering is enabled.
  • EnableSessionState: Determines whether SessionState is enabled.
  • ErrorPage: Determines the URL to be redirected to in the event of an unhandled exception.

ASP .NET Controls

These controls form the heart of ASP .NET. These are rich server side controls that can be used to build a powerful UI on the web similar to Windows based user interfaces.

<asp:label id=lblName runat="server">Please enter your name</asp:label>
<asp:label id=lblHello runat="server"></asp:label>
<asp:textbox id=txtName runat="server"></asp:textbox>
<asp:button id=btnSubmit runat="server" Text="Submit"></asp:button>

As you can see in the code above, the HelloWorld example uses four ASP .Net controls. Note the "asp:" prefix for each control. This is what distinguishes ASP .NET controls from standard HTML controls. We use two labels to display static messages, a textbox to allow the user to input the name, and a button that will be used as a submit button. These server side controls fire events very similar to the way windows controls fire events in a VB 6.0 program. These events can be trapped and code can be written to take action depending on w

[1] [2] [3]  下一页


[常用软件]blog-Hello实现即时通信图片分享  [常用软件]Google即时通讯软件Hello试用心得
[Delphi程序]DELPHI6抢先研究:WebService/SOAP版的Hello worl…  [Delphi程序]从 Form1.Caption = “Hello World”说起
[Web开发]彻底放弃IIS让Apache也支持ASP.NET  [Web开发]ASP.NET 2.0发送电子邮件中存在的问题
[Web开发]ASP.NET 2.0移动开发之属性重写和模板化  [Web开发]ASP.NET 2.0中实现模板中的数据绑定
[Web开发]ASP.NET 母版页概述  [Web开发]ASP.NET 2.0 ObjectDataSource控件
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · Web开发  · 网页制作
    · 平面设计  · 网站运营
    · 网站推广  · 搜索优化
    · 建站心得  · 站长故事
    · 互联动态
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台