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

Hello World:解剖 ASP.NET 项目

作者:闵涛 文章来源:闵涛的学习笔记 点击数:2050 更新时间:2009/4/23 10:48:40
hat event is fired for what control. This offers much more functionality and flexibility that standard HTML controls. Currently ASP .NET supports the following types on server side controls:

  • <asp:button>
  • <asp:imagebutton>
  • <asp:linkbutton>
  • <asp:hyperlink>
  • <asp:textbox>
  • <asp:checkbox>
  • <asp:radiobutton>
  • <asp:image>
  • <asp:label>
  • <asp:panel>
  • <asp:table>

HelloWorld.vb Details

This file contains the code that supports the ASP .NET controls from the HelloWorld.aspx file. This code is identical to VB code, in fact it is VB code. Lets start with some code excerpts, again I would recommend you to have VS .NET open and the project loaded and have the code in its entirety.

Importing Namespaces

Imports System
Imports System.ComponentModel.Design
Imports System.Data
Imports System.Drawing
Imports System.Web
Imports System.Web.SessionState
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls
Imports Microsoft.VisualBasic

.NET introduces the concept of namespaces. Namespaces are essentially a collection of types and functionality. VB .NET introduces the Imports statement that is used to references assemblies outside your project. Most of the namespaces that are imported here are from the System namespace, but you could import any namespace as long as it lives in a public assembly. Importing a namespace is similar to adding a reference to a DLL in VB 6.0. It allows you to access the public elements (classes, functions, methods, etc) in that namespace. In our example, VS .NET automatically imports these namespaces to add support for Web Controls. You could easily add to this default list. For example, if you wanted to add XML manipulation support to your code, you can add the "Imports System.XML" line to your code.

HelloWorld Class

Public Class HelloWorld

    Inherits System.Web.UI.Page
    Protected WithEvents lblName As System.Web.UI.WebControls.Label
    Protected WithEvents btnSubmit As System.Web.UI.WebControls.Button
    Protected WithEvents txtName As System.Web.UI.WebControls.TextBox
    Protected WithEvents lblHello As System.Web.UI.WebControls.Label

    Public Sub btnSubmit_Click(ByVal sender As Object, ByVal e As 
                                                      System.EventArgs)
        txtName.Visible = False
        lblName.Visible = False
        btnSubmit.Visible = False
        lblHello.Text = "Hello World (by " + txtName.Text + ")"
        lblHello.Visible = True
    End Sub
    
    Protected Sub WebForm1_Load(ByVal Sender As System.Object, ByVal e
                                                   As System.EventArgs)
        '''' Evals true first time browser hits the page
        If Not IsPostback Then
            lblHello.Visible = False
        End If
    End Sub
    
End Class

VB .NET is a purely object oriented language. (Of course, you could still write non-OO code and not use the OO features that VB .NET provides, but that defeats the purpose). Thus, all the code in the above figure resides in a class definition. We declare a public class named HelloWorld that forms the backbone of our HelloWorld.aspx page. All the code behind an ASP .NET page is derived from the System.Web.UI.Page class, which contains the framework code for ASP .NET pages.

The four controls that are used on this page as defined as protected members of this class and the type of each controls is referenced from the System.Web.UI.WebControls namespace. The WithEvents keyword is used to specify that the object variables will respond to triggered events. In addition to these data members, you could add any data members that you may want to and specify the access modifiers (public, private, protected) for these data members.

The crux of the logic in an ASP .NET page will live in the event handling code for one or more server side controls. In our trivial example, we handle two events. The first event is for the page load in which we simply hide the label that will be used to display the Hello World message. When the user clicks on the Submit button, this will fire the event handled by the btnSubmit_Click method. In this method, we simply read the value entered in the textbox and display the Hello World message. We also hide some of the controls that are no longer needed. This is all the code that is necessary to support our Hello World sample.

Config.web Details

With ASP .NET, Microsoft introduces the notion of a human readable and modifiable configuration file for Web Applications. Those of you who have dealt with the IIS Metabase know that maintaining and modifying configuration wasn''''t exactly a breeze. ASP .NET introduces an XML file based configuration scheme, which is extremely powerful. The amount of details in the file warrants a whole new article, so I will focus only on some of the settings that are generated by VS .NET by default.

<compliation  debugmode="true" />
<customerrors mode="Off"  />
<trace
     enabled="false"
     requestlimit="0"
     pageoutput="false"
     tracemode="SortByTime"
/>
<sessiostate
     inproc="true"
     usesqlserver="false"
     cookieless="false"
     timeout="20"
     server="localhost"
     port="42424"
/>

These are some of the default settings generated by VS .NET.

Compilation Mode
This settings tells the.NET compilers to generate debug information for the .aspx files. This is usually set to True in the development cycle, but once the application is moved over to Production, this should be set to false. Turning off this setting greatly improves the runtime performance of the system.
Custom Errors
ASP .NET introduces a declarative way of exception handling. By turning on CustomErrors, it is possible for ASP .NET to redirect the page to an error page whenever an unhandled exception occurs. Further, it is possible to define separate custom error pages for each HTTP error code that could occur. This is a very powerful feature that can avoid ugly errors being shown to the user.
Trace
Those of you who have tried debugging traditional ASP programs probably used more Response.Write statements than you cared to. ASP .NET introduces an elegant way of adding trace\debug information that is consistent and configurable. By turning on Trace in the Config.web file, it is possible to have Trace statements throughout the code that can be turned on or off by this switch. Further, you can determine where the trace output is displayed and have levels of tracing such as Fatal or Informational.
Session State
There have been so many occasions where I wished I could use the ASP Session variables, only to realize that the application will be running on a WebFarm without server affinity. In these cases, you end up not using the ASP Session,

上一页  [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……
    咸宁网络警察报警平台