打印本文 打印本文 关闭窗口 关闭窗口
Hello World:解剖 ASP.NET 项目
作者:武汉SEO闵涛  文章来源:敏韬网  点击数2767  更新时间:2009/4/23 10:48:40  文章录入:mintao  责任编辑:mintao
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]  下一页

打印本文 打印本文 关闭窗口 关闭窗口