下面的代码示例演示如何在代码隐藏页模型中使用 Page 类。注意,代码隐藏源文件声明了一个从基页类继承的分部类。基页类可以是 Page,也可以是从 Page 派生的其他类。另外请注意,分部类允许代码隐藏文件使用页中定义的控件,而无需将其定义为字段成员。
Imports System
Partial Class MyCodeBehindVB
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
' Place page-specific code here.
End Sub
' Define a handler for the button click.
Protected Sub SubmitBtn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyButton.Click
MySpan.InnerHtml = "Hello, " + MyTextBox.Text + "."
End Sub
End Class
using System;
public partial class MyCodeBehindCS : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// Place page-specific code here.
}
// Define a handler for the button click.
protected void SubmitBtn_Click(object sender, EventArgs e)
{
MySpan.InnerHtml = "Hello, " + MyTextBox.Text + ".";
}
}
下面的代码示例演示与前面的代码隐藏源文件对应的 .aspx 文件。
<%@ Page Language="VB" CodeFile="pageexample.aspx.vb" Inherits="MyCodeBehindVB" %>
<html>
<head runat="server">
<title>Page Class Example</title>
</head>
<body>
<form runat="server">
<div>
<table>
<tr>
<td> Name: </td>
<td> <asp:textbox id="MyTextBox" runat="server"/> </td>
</tr>
<tr>
<td></td>
<td><asp:button id="MyButton" text="Click Here" onclick="SubmitBtn_Click" runat="server"/></td>
</tr>
<tr>
<td></td>
<td><span id="MySpan" runat="server" /></td>
</tr>
</table>
</div>
</form>
</body>
</html>
<%@ Page Language="C#" CodeFile="pageexample.aspx.cs" Inherits="MyCodeBehindCS" %>
<html>
<head runat="server">
<title>Page Class Example</title>
</head>
<body>
<form runat="server">
<div>
<table>
<tr>
<td> Name: </td>
<td> <asp:textbox id="MyTextBox" runat="server"/> </td>
</tr>
<tr>
<td></td>
<td><asp:button id="MyButton" text="Click Here" onclick="SubmitBtn_Click" runat="server"/></td>
</tr>
<tr>
<td></td>
<td><span id="MySpan" runat="server" /></td>
</tr>
</table>
</div>
</form>
</body>
</html>
必须使用 @ Page 指令并使用 Inherits 和 CodeFile 属性将代码隐藏文件链接至 .aspx 文件。在此示例中,Inherits 属性指示 MyCodeBehind 类,CodeFile 属性指示包含该类的语言特定的文件的路径。
下面的代码示例演示单文件页模型以及如何访问 Page 的 IsPostBack 属性和 Response 属性。
<%@ Page Language="VB" %>
<script runat="server">
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Dim sb As New StringBuilder()
If (Page.IsPostBack) Then
sb.Append("You posted back to the page.<br>")
End If
sb.Append("The host address is " + Page.Request.UserHostAddress + ".<br>")
sb.Append("The page title is """ + Page.Header.Title + """.")
PageMessage.Text = sb.ToString()
End Sub
</script>
<html>
<head runat="server">
<title>Page Class Example</title>
</head>
<body>
<form id="form1"
runat="server">
<div>
<asp:Label id="PageMessage"
runat="server"/>
<br /> <br />
<asp:Button id="PageButton"
Text="PostBack"
runat="server" />
</div>
</form>
</body>
</html>
<%@ Page Language="C#" %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder();
if (Page.IsPostBack)
sb.Append("You posted back to the page.<br>");
sb.Append("The host address is " + Page.Request.UserHostAddress + ".<br>");
sb.Append("The page title is \"" + Page.Header.Title + "\".");
PageMessage.Text = sb.ToString();
}
</script>
<html>
<head runat="server">
<title>Page Class Example</title>
</head>
<body>
<form id="form1"
runat="server">
<div>
<asp:Label id="PageMessage"
runat="server"/>
<br /> <br />
<asp:Button id="PageButton"
Text="PostBack"
runat="server" />
</div>
</form>
</body>
</html>