The following code example demonstrates how the Page class is used in the code-behind page model. Note that the code-behind source file declares a partial class that inherits from a base page class. The base page class can be Page, or it can be another class that derives from Page. Furthermore, note that the partial class allows the code-behind file to use controls defined on the page without the need to define them as field members.
|
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 + ".";
}
}
|
The following code example shows the .aspx file that corresponds to the preceding code-behind source file.
Security Note: |
|---|
This example has a text box that accepts user input, which is a potential security threat. By default, ASP.NET Web pages validate that user input does not include script or HTML elements. For more information, see
Script Exploits Overview.
|
|
<%@ Page Language="VB" CodeFile="pageexample.aspx.vb" Inherits="MyCodeBehindVB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html >
<head runat="server">
<title>Page Class Example</title>
</head>
<body>
<form id="form1" 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" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html >
<head runat="server">
<title>Page Class Example</title>
</head>
<body>
<form id="form1" 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>
|
You must use the @ Page directive and use the Inherits and CodeFile attributes to link the code-behind file to the .aspx file. In this example, the Inherits attribute indicates the MyCodeBehind class and the CodeFile attribute indicates the path to the language-specific file that contains the class.
The following code example demonstrates the single-file page model and how to access the IsPostBack property and the Response property of the Page.
|
<%@ 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)
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#" %>
<!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)
{
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>
|