ASP.NET Tutorial/ASP.Net Instroduction/Code behind
Содержание
code-behind model allows for code separation of the page"s business logic from its presentation logic
presentation logic for the page is stored in an .aspx page, whereas the logic piece is stored in a separate class file: .aspx.vb or .aspx.cs.
File Options Using Code-Behind File Created
Web Form .aspx file
.aspx.vb or .aspx.cs file
Master Page .master file
.master.vb or .master.cs file
Web User Control .ascx file
.ascx.vb or .ascx.cs file
Web Service .asmx file
.asmx.vb or .asmx.cs file
An .aspx page that uses the ASP.NET 2.0 code-behind model (VB version)
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb"
Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Simple Page</title>
</head>
<body>
<form runat="server">
What is your name?<br />
<asp:Textbox ID="Textbox1" Runat="server"></asp:Textbox><br />
<asp:Button ID="Button1" Runat="server" Text="Submit"
OnClick="Button1_Click" />
<asp:Label ID="Label1" Runat="server"></asp:Label>
</form>
</body>
</html>
A code-behind page (VB version)
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Button1_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Label1.Text = "Hello " & TextBox1.Text
End Sub
End Class
An .aspx page that uses the ASP.NET 2.0 code-behind model (C# version)
<%@ Page Language="C#" CodeFile="Default.aspx.cs" Inherits="_aspx" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Simple Page</title>
</head>
<body>
<form runat="server">
What is your name?<br />
<asp:Textbox ID="Textbox1" Runat="server"></asp:Textbox><br />
<asp:Button ID="Button1" Runat="server" Text="Submit"
OnClick="Button1_Click" />
<asp:Label ID="Label1" Runat="server"></asp:Label>
</form>
</body>
</html>
A code-behind page (C# version)
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = "Hello " + Textbox1.Text;
}
}
Code behind (VB.net)
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="FirstPageCodeBehind" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>First Page Code-Behind</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button
id="Button1"
Text="Click Here"
Runat="server" />
<br /><br />
<asp:Label
id="Label1"
Runat="server" />
</div>
</form>
</body>
</html>
File: Default.aspx.vb
Partial Class FirstPageCodeBehind
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Label1.Text = "Click the Button"
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Label1.Text = "Thanks!"
End Sub
End Class
code-inline model: all the code is contained within a single .aspx page
File Options Using Inline Coding File Created
Web Form .aspx file
Master Page .master file
Web User Control .ascx file
Web Service .asmx file
File: Default.aspx (VB)
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<script runat="server">
Protected Sub Button1_Click(ByVal sender As Object, _
ByVal e As System.EventArgs)
Label1.Text = "Hello " & Textbox1.Text
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Simple Page</title>
</head>
<body>
<form runat="server">
What is your name?<br />
<asp:Textbox ID="Textbox1" Runat="server"></asp:Textbox><br />
<asp:Button ID="Button1" Runat="server" Text="Submit"
OnClick="Button1_Click" />
<asp:Label ID="Label1" Runat="server"></asp:Label>
</form>
</body>
</html>
File: Default.aspx (C#)
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<script runat="server">
protected void Button1_Click(object sender, System.EventArgs e)
{
Label1.Text = "Hello " + Textbox1.Text;
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Simple Page</title>
</head>
<body>
<form runat="server">
What is your name?<br />
<asp:Textbox ID="Textbox1" Runat="server"></asp:Textbox><br />
<asp:Button ID="Button1" Runat="server" Text="Submit"
OnClick="Button1_Click" />
<asp:Label ID="Label1" Runat="server"></asp:Label>
</form>
</body>
</html>
Use Code behind
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="FirstPageCodeBehind" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>First Page Code-Behind</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button
id="Button1"
Text="Click Here"
OnClick="Button1_Click"
Runat="server" />
<br /><br />
<asp:Label
id="Label1"
Runat="server" />
</div>
</form>
</body>
</html>
File: Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class FirstPageCodeBehind : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = "Click the Button";
}
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = "Thanks!";
}
}