| 悠游在线( yousoft.hi.com.cn ) 作者:徐长友
一般网站建设通常要求开发人员做后台的程序设计,前面有专业的美工做界面设计。虽然有时候开发人员也会做些界面设计,但是通常都无法达到专业的要求。在以前的ASP中,由于代码和HTML页面语言混杂在一起,这就使得网站的建设变得相当的困难。但在ASP.NET中,这种情况完全改变了。 下面就用C# Builder建一个简单的ASP.NET应用程序。
打开C# Builder,选择 File>New>other…菜单项,你将会看到下面的窗口:
我们选择C# ASP Projects,你就会看到右边有3种可供选择。我们选择ASP.NET Web Application,就可以开始创建我们第一个ASP.NET应用程序。C# Builder会自动在Web程序所在地wwwroot目录下面创建一个虚拟目录,在这里叫做WebApplication1,你可以根据程序的功能取一个其它的什么名字。这时打开IIS,你就会发现产生了一个名为WebApplication1的虚拟目录,如果没有自动创建的话,自己用IIS创建一个虚拟目录。
你会看到C# Builder自动帮你建立了一个名为WebForm1的Web page。这里放上一个label和一个按钮。(注:是HTML Label和HTML Button),如图:
按F9运行程序,你就会看到你的一个简单的ASP.NET Web页面。是不是很简单?
以下是我们建的HTML页面
1@ Page language="c#" Debug="true" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="myaspx.WebForm1"
1<html>
2<head>
3<title></title>
4<meta content="Borland ASP.NET Designer for c# Package Library 7.1" name="GENERATOR"/>
5</head>
6<body ms_positioning="GridLayout">
7<form runat="server">
8<input size="25" style="Z-INDEX: 2; LEFT: 246px; WIDTH: 75px; POSITION: absolute; TOP: 150px; HEIGHT: 24px" type="button" value="OK"/><span style="Z-INDEX: 1; LEFT: 86px; WIDTH: 187px; POSITION: absolute; TOP: 86px; HEIGHT: 27px" title="">我的ASP.NET程序!</span><font color="#ccffcc"></font>
9</form>
10</body>
11</html>
从第一行中我们可以看出,该页面的后台代码全都在WebForm1.aspx.cs文件中。我们可以在这个文件中编写程序,生成的代码如下:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace myaspx
{
///
1<summary>
2/// Summary description for WebForm1.
3/// </summary>
public class WebForm1 : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
///
1<summary>
2/// Required method for Designer support - do not modify
3/// the contents of this method with the code editor.
4/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
}
}