ASP.NET 中的代码块
更新:2007 年 11 月
ASP.NET 单文件页模型与 Active Server Pages (ASP) 页结构类似,因为包含代码的脚本块出现在 HTML 之前,并且可以在 HTML 中使用特殊标记。本主题说明将 ASP 代码更新为 ASP.NET 会涉及到的问题。
ASP.NET 代码隐藏页模型将脚本块代码与 HTML 和 ASP.NET 标记分开。有关更多信息,请参见 ASP.NET 网页语法概述。有关可使您将控件属性值绑定到数据的数据绑定语法的信息,请参见数据绑定表达式概述。
声明变量和过程
所有 ASP.NET 过程和全局变量都应在 <script runat="server"> 开始标记之前的 <script runat="server"> 块中声明,而不能在 ASP <%...%> 样式分隔符之间的块中声明。您仍可以在 <%...%> 呈现块中声明变量,但只有该页上的其他呈现块可以访问它们,其他函数或过程不能全局访问它们。一页可以包含多个 script 块,但一页上的所有块中的编程语言必须相同。
下面的代码示例是在脚本和呈现块中声明变量和过程的 ASP.NET 页的代码。如果通过 URL 将查询字符串传递给 ASP.NET 页,则查询字符串会显示在该页上。
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
' The following variables are visible to all procedures
' within the <script> block.
Dim str As String
Dim i, i2 As Integer
Function DoubleIt(ByVal inpt As Integer) As Integer
' The following variable is visible only within
' the DoubleIt procedure.
Dim factor As Integer = 2
DoubleIt = inpt * factor
End Function
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<%
' The following local variable is visible only within this
' and other render blocks on the page.
Dim myVar
myVar = Request.QueryString
Response.Write("The querystring is " _
& Server.HtmlEncode(myVar.ToString()))
%>
</div>
</form>
</body>
</html>
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
// The following variables are visible to all procedures
// within the <script> block.
String str;
int i;
int i2;
int DoubleIt(int inpt)
{
// The following variable is visible only within
// the DoubleIt procedure.
int factor = 2;
return inpt * factor;
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<%
// The following local variable is visible only within this
// and other render blocks on the page.
object myVar;
myVar = Request.QueryString;
Response.Write("The querystring is " +
Server.HtmlEncode(myVar.ToString()));
%>
</div>
</form>
</body>
</html>
呈现文本
ASP.NET 不支持呈现函数。使用早期版本的 ASP,可以在过程体中插入文本 HTML,如下面的代码示例所示。
<% Sub SomeProcedure() %>
<H3> Render this line of bold text. </H3>
<% End Sub %>
这些代码会在 ASP.NET 页中生成错误。对于 ASP.NET,您必须编写如下代码。
<script runat=server>
Sub SomeProcedure()
Response.Write("<H3> Render this line in bold text.</H3>")
End Sub
</script>
除全局变量声明外,包含在 <script>…</script> 标记中的所有代码都必须封装在过程内部。
页处理、加载和卸载
对于 ASP,代码放在 <%...%> 标记内,页处理从第一个 <%> 标记后面的第一个语句开始。对于 ASP.NET,所有要在加载页后立即处理的代码都必须包含在 Page_Load 内部事件中。您仍可以在 <%...%> 块中编写代码,但它将在加载页后,在呈现时将以由上而下的方式(像在 ASP 中一样)执行。如果需要执行初始化代码,则它应出现在 Page_Load 事件中,该事件在 ASP.NET 引擎加载该页后立即引发,如下面的代码示例所示。
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
' Place all startup code here, including initialization of
' variables, preloading of controls, and loading of
' data from a database.
If Page.IsPostBack Then
Response.Write("<br />Page has been posted back.")
End If
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
Hello.<br />
<asp:Button ID="Button1" runat="server"
Text="Click to Post Back" />
</div>
</form>
</body>
</html>
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
Response.Write("<br />Page has been posted back.");
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
Hello.<br />
<asp:Button ID="Button1" runat="server"
Text="Click to Post Back" />
</div>
</form>
</body>
</html>
相关的 Page_Unload 内部事件始终是页执行生命周期中引发的最后一个事件,并可以用于执行任何页清理代码。
有关更多信息,请参见 ASP.NET 页生命周期概述 和 ASP.NET Web 服务器控件事件模型。