• ASP.NET 页框架是一种编程框架,它在
Web 服务器上运行以动态地生成和管理
Web 窗体页。可以使用Web 窗体页来创建
可编程的Web 页,这些Web 页用作Web
应用程序的用户界面。
• Web 窗体页在任何浏览器或客户端设备中
向用户提供信息,并使用服务器端代码来
实现应用程序逻辑。
• Web 窗体页输出几乎可以包含任何支持
HTTP 的语言(包括HTML、XML、WML
和ECMAScript (JScript, JavaScript))。
Web窗体页的特点
• 基于Microsoft ASP.NET 技术。在该技术中,在
服务器上运行的代码动态地生成到浏览器或客户
端设备的Web 页输出。
• 兼容所有浏览器或移动设备。Web 窗体页自动为
样式、布局等功能呈现正确的、符合浏览器的
HTML。
• 兼容.NET 公共语言运行库所支持的任何语言
• 基于Microsoft .NET 框架生成
• 在Visual Studio 中通过强大的快速应用程序开发
(RAD) 工具受到支持
• 具有灵活性,因为您可以向它们添加用户创建的
控件和第三方控件。
单文件Web 窗体页
• 除了由.aspx 文件和单独的类文件组成的页之
外,ASP.NET 结构还支持“单文件”模型,在此模
型中,UI 元素和代码位于同一文件中。
• 单文件页的处理方式存在一些不同之处:
– 不会将该页的代码编译成一个单独的类,然后从该类
派生.aspx 文件。相反,.aspx 文件将直接从Page 类
派生。
– 当部署该页时,由于源代码在物理上位于.aspx 文件
中,所以源代码将与Web 窗体页一起部署
WEB页面处理过程
• 页面的一次往返处理:用户对Server
Control的一次操作,就可能引起页面的一次往
返处理:页面被提交到服务器端,执行响应的
事件处理代码,重建页面,然后返回到客户端
• 页面重建:每一次页面被请求,或者页面
事件被提交到服务器,asp.net运行环境将执行
必要的代码,重建整个页面,把结果页面送到
浏览器,然后抛弃页面的变量、控件的状态和
属性等等页面信息。
页面事件
以下列表按激发顺序提供运行时连线的代理实例:
• Page_Init:初始化值或连接
• Page_Load:出现此事件期间,您可以执行一系
列的操作来首次创建ASP.NET 页面或响应由投
递引起的客户端事件。在此事件之前,已还原页
面和控件视图状态。使用IsPostBack 页面属性检
查是否为首次处理该页面。如果是首次处理,请
执行数据绑定。此外,请读取并更新控件属性。
• Page_DataBind:在页面级别调用DataBind ,
也可在单个控件上调用
DataBindPage_PreRender:恰好在保存视图状
态和呈现控件之前激发PreRender 事件。
• Page_Unload:此事件是执行最终清理工作的合
适位置。
非确定性事件
• Page_Error:如果在页面处理过程中出现
未处理的例外,则激发Error 事件。错误事
件为您提供了妥善处理错误的机会。
• Page_AbortTransaction:如果要指明交易
是成功还是失败,交易事件非常有用。此
事件通常用于购物车方案,其中此事件可
以指示订购是成功还是失败。如果已终止
交易,则激发此事件。
• Page_CommitTransaction:如果已成功提
交交易,则激发此事件。
Global.asax
Gobal.asax是干什么的?
Global.asax文件也称为ASP.NET应用程序
文件,它一般被放在根目录下。此文件中的
代码不产生用户界面,也不相应单个页面的
请求。
它主要是负责处理Application_Start,
Application_End,Session_Start和
Session_End事件的。
Application
Application和Session事件
• 当Application 对象的生命周期开始时,
Application_Start 事件会被启动,当Application
对象的生命周期结束时Application_End 事件会
被启动
• 当有一个新用户访问应用程序时,就会立刻触发
Session_Start事件。当某个用户停止了访问或者
程序执行了Session.Abandon方法,就会触发
Session_End事件。
• 一个Application_End事件肯定发生在
Session_End事件之后,Application_End事件只
有在服务器停止工作或Application_End事件卸
载时才触发。
Global.asax中的事件
Application_Error 发生错误时激发
Application_End 应用程序结束时激发
Session_End 会话结束时激发
Application_EndRequest HTTP请求结束时激发
Session_Start 会话启动时激发
Application_AuthenticateRequest 应用程序批准HTTP请求时激发
Application_BeginRequest HTTP请求开始时激发
Application_Start 应用程序启动时激发
using System;
using System.Collections;
using System.ComponentModel;
using System.Web;
using System.Web.SessionState;
using System.IO;
namespace ASPNETPage
{
/// <summary>
/// Global 的摘要说明。
/// </summary>
public class Global : System.Web.HttpApplication
{
public Global()
{
InitializeComponent();
}
protected void Application_Start(Object sender, EventArgs e)
{
// StreamReader rd = new StreamReader(Server.MapPath("counter.txt"));
// int nNum = int.Parse(rd.ReadLine());
// Application.Lock();
// Application["Counter"] = nNum;
// Application.UnLock();
// rd.Close();
}
protected void Session_Start(Object sender, EventArgs e)
{
// Application.Lock();
// Application["Counter"] = Convert.ToInt32(Application["Counter"])+1;
// Application.UnLock();
// //写入
// StreamWriter sw = new StreamWriter(Server.MapPath("counter.txt"),false);//false为不追加
// sw.WriteLine(Application["Counter"]);
// sw.Close();
}
protected void Application_BeginRequest(Object sender, EventArgs e)
{
}
protected void Application_EndRequest(Object sender, EventArgs e)
{
}
protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{
}
protected void Application_Error(Object sender, EventArgs e)
{
}
protected void Session_End(Object sender, EventArgs e)
{
}
protected void Application_End(Object sender, EventArgs e)
{
// //写入
// StreamWriter sw = new StreamWriter(Server.MapPath("counter.txt"),false);//false为不追加
// sw.WriteLine(Application["Counter"]);
// sw.Close();
}
Web Form Designer generated code
}
}

页面指令
• 页面指令指定页面编译器在处理文件时使用的可选
设置。页面指令位于页面文件的顶部,使用以下语
法:
– <%@ directive {attribute=value}* %>
• ASP.NET 页面支持下列指令:
– @ Page
– @ Implements
– @ Import
– @ Register
– @ Assembly
– @ Reference
– @ OutputCache
@import
• 命名空间的导入
• 示例:
– <%@ import namespace=“System.NET”%>
– 导入类库
– 有点相似于#include <stdio.h>
@register
• 自定义控件声明
• 示例如下:
– <%@ Register TagPrefix="saidy"
TagName="info" Src="con01.ascx" %>
@page
• 语法形式如下:
– <%@ Page Language="VB"
ContentType="text/html"
ResponseEncoding="gb2312" %>
• 参数
– Language
• 示例:language=“vb”
• 示例:language=“c#”
@page
• 参数
– Debug
• 指示是否应使用调试符号编译该页。如果应使用调
试符号编译该页,则为true;否则为false。
• 示例:debug=“true”
– Buffer
• 确定是否启用HTTP 响应缓冲。如果启用页缓冲,
则为true;否则为false。默认值为true。
• 示例:buffer=true
• 参数
– AutoEventWireup
• 指示页的事件是否自动连网。如果启用事件自动连
网,则为true;否则为false。默认值为true。
– ErrorPage
• 定义在出现未处理页异常时用于重定向的目标URL。
• Errorpage=error.htm
内联编程模型与代码隐藏编程模型
ASP.NET 支持两种模式的页面开发:
• 在.aspx 文件的<script runat=server> 块
内写入页面逻辑代码,然后在服务器上首
次请求该页面时动态编译代码。
• 在外部类中写入页面逻辑代码,在服务器
上进行部署之前编译该外部类,并且运行
时在.aspx 文件“背后”链接该类。
内联编程模型的限制
• 不能直接在Visual Studio 中创建单文件Web 窗
体页
• 不能通过从工具箱中拖动将非可视组件(如数据
组件)添加到该页上
• 在HTML 视图中而不是在代码编辑器中编写代码。
• 您得不到语法检查或语句结束、制表位设置或代
码格式设置等功能。
• 必须手动将事件绑定到事件处理程序
• 不支持某些调试功能
• 在运行该页之前不会捕获编译时错误。
CodeBehind技术
• Code Behind就是所谓的代码分离,自从
Microsoft公司推出了ASP.NET以后,Code
Behind就是一个热门的话题。在一般的
ASP.NET文件中,Code Behind主要是用
二个文件来创建一个ASP.NET的页面,其
中一个是设计文件,一般以.aspx或者.ascx
做为扩展名,而另外一个是程序代码文
件,一般以.vb或者.cs做为扩展名,其程序
设计语言主要是VB.Net或者是C#。
CodeBehind的优点
• Code Behind把界面设计代码和程序设计代
码以不同的文件分开,对于代码的重复使
用,程序的调试和维护都是革命性的
• 当发布网站的时候,可以利用这种技术来
有效的保护代码。这对于程序的安全性是
一个提高。
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
public class BasePage : Page
{
protected Label eMail;
protected Label siteName;
protected string strID ="1";
virtual protected void PageLoadEvent(object sender, System.EventArgs e)
{}
protected void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
string name = strID;//Context.User.Identity.Name;
eMail.Text = DatabaseGateway.RetrieveAddress(name);
siteName.Text = "Microsoft-site";
PageLoadEvent(sender, e);
}
}
Web Form Designer generated code
} 

<table width="100%" cellspacing="0" cellpadding="0">
<tr>
<td align="right" bgcolor="#9c0001" cellspacing="0" cellpadding="0" width="100%" height="20">
<font size="2" color="#ffffff">欢迎:
<asp:Label id="eMail" runat="server">username</asp:Label> </font>
</td>
</tr>
<tr>
<td align="right" width="100%" bgcolor="#d3c9c7" height="70">
<font size="6" color="#ffffff">
<asp:Label id="siteName" Runat="server">Microsoft-site Banner</asp:Label> </font>
</td>
</tr>
</table> 
using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
public class DatabaseGateway
{
public static string RetrieveAddress(string name)
{
string address = null;
string selectCmd = string.Format("select * from tbstudentinfo where (studentid = '{0}')", name);
string strCon = System.Configuration.ConfigurationSettings.AppSettings["DSN"];
SqlConnection myConnection = new SqlConnection(strCon);
SqlDataAdapter myCommand = new SqlDataAdapter(selectCmd, myConnection);
DataSet ds = new DataSet();
myCommand.Fill(ds,"webuser");
if(ds.Tables["webuser"].Rows.Count == 1)
{
DataRow row = ds.Tables["webuser"].Rows[0];
address = row["Email"].ToString();
}
return address;
}
} 
<%@ Page language="c#" Codebehind="Main.aspx.cs" AutoEventWireup="false" Inherits="ASPNETPage.PageController.Main" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>Main</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<!-- #include virtual="BasePage.inc" -->
<form id="Main" method="post" runat="server">
<asp:Label id="pageNumber" style="Z-INDEX: 101; LEFT: 109px; POSITION: absolute; TOP: 231px" runat="server" Width="157px">Label</asp:Label>
<asp:HyperLink id="HyperLink1" style="Z-INDEX: 102; LEFT: 305px; POSITION: absolute; TOP: 233px" runat="server" NavigateUrl="Second.aspx">下一页</asp:HyperLink>
</form>
</body>
</HTML>
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 ASPNETPage.PageController
{
/// <summary>
/// Main 的摘要说明。
/// </summary>
public class Main :BasePage
{
protected System.Web.UI.WebControls.HyperLink HyperLink1;
protected System.Web.UI.WebControls.Label pageNumber;
protected override void PageLoadEvent(object sender, System.EventArgs e)
{
pageNumber.Text = "第一页";
}
Web Form Designer generated code

}
}
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 ASPNETPage._01PageController
{
/// <summary>
/// Second 的摘要说明。
/// </summary>
public class Second : BasePage
{
protected System.Web.UI.WebControls.Label pageNumber;
protected override void PageLoadEvent(object sender, System.EventArgs e)
{
pageNumber.Text = "第二页";
} 
Web Form Designer generated code
}
}


浙公网安备 33010602011771号