Building a Web Application->Code Behind vs. Code Inline
ASP.NET provides two ways //两种方式,两种途径that you can organize code //组织代码within your pages.
Note that in the preceding example //前面的例子,上面的例子the event handler//事件处理器 for the Button was located between
The advantage of the simplified //简单化的code-behind model //代码分离模型over//比 previous versions is that you do not need to maintain //维护separate declarations//声明 of server control variables//服务器端控件变量 in the code-behind class.//代码分离类 Using partial classes //部分类(new in 2.0) allows the server control IDs of the ASPX page to be accessed directly//被直接访问 in the code-behind file//代码分离页面. This greatly simplifies//简化 the maintenance//维护 of code-behind pages//代码分离页面.
Inline Code Separation//在线的代码分离
The example below demonstrates //举例说明a simple ASP.NET page with three server controls//服务器端控件, a TextBox, Button, and a Label. Initially//最初,开头 these controls just render //表现,描述their HTML form equivalents//HTML形式的等价形式. However, when a value is typed in the TextBox and the Button is clicked on the client//客户端, the page posts back to//反馈回 the server //服务器and the page handles this click event//点击事件 in the code of the page//页面的代码部分, dynamically //动态地updating //更新the Text property //文体属性,文本性质of the Label control. The page then re-renders//重新表现,再次表现 to reflect //反映the updated //更新后的text. This simple example demonstrates the basic mechanics//基本原理 behind the server control model //服务器端控件模型后面的that has made ASP.NET one of the easiest Web programming models//编程模型 to learn and master//掌握. <这个让asp.net 是最容易学习和掌握的编程模型>C# Inline Code Separation
<%@ page language="C#" %>

<script runat="server">

void Button1_Click(object sender, EventArgs e)
{
Label1.Text = "Hello " + TextBox1.Text;
}
</script>

<html>
<head>
<title>ASP.NET Inline Pages</title>
</head>
<body>
<form id="Form1" runat="server">
<h1>Welcome to ASP.NET 2.0!</h1>
<b>Enter Your Name:</b>
<asp:TextBox ID="TextBox1" Runat="server"/>
<asp:Button ID="Button1" Text="Click Me" OnClick="Button1_Click" Runat="server"/>
<br />
<br />
<asp:Label ID="Label1" Text="Hello" Runat="server" />
</form>
</body>
</html>
<%@ page language="C#" %>
<script runat="server">
void Button1_Click(object sender, EventArgs e)
{
Label1.Text = "Hello " + TextBox1.Text;
}
</script>
<html>
<head>
<title>ASP.NET Inline Pages</title>
</head>
<body>
<form id="Form1" runat="server">
<h1>Welcome to ASP.NET 2.0!</h1>
<b>Enter Your Name:</b>
<asp:TextBox ID="TextBox1" Runat="server"/>
<asp:Button ID="Button1" Text="Click Me" OnClick="Button1_Click" Runat="server"/>
<br />
<br />
<asp:Label ID="Label1" Text="Hello" Runat="server" />
</form>
</body>
</html>
Note that in the preceding example //前面的例子,上面的例子the event handler//事件处理器 for the Button was located between
<script></script> tags //标签in the same page//同一页面 containing //包含the server controls//服务器端控件. ASP.NET calls this type of page programming code-inline//代码内联模型, and it is very useful when you want to maintain //维护your code and presentation logic//表现逻辑 in a single file//在一个文件中. However, ASP.NET also supports another way to factor your code//代码 and presentation content//表现内容, called the code-behind model代码分离模型. When using code-behind, the code for handling events is located in a physically//物理上 separate file//分离的文件 from the page that contains server controls and markup//服务器控件和标记. This clear delineation //.界线轮廓between code and content//代码和内容 is useful when you need to maintain //维护these separately, such as when more than one person is involved in //加入,卷入creating the application. It is often common//正常的 in group projects //团体项目to have designers working on the UI portions //界面部分of an application//应用程序 while developers work on the behavior //行为or code. The code-behind model//代码分离模型 is well-suited to//很适合 that environment.
Simplified Code Behind Model //简化的代码分离模型New in 2.0
ASP.NET 2.0 introduces an improved//改进的 runtime for code-behind pages//代码分离页面 that simplifies the connections//连接 between the page and code. In this new code-behind model, the page is declared as a partial //部分的class//部分类<像人的身体的不同部分然后组合成一个完整的人>, which enables both the page and code files to be compiled into a single class //一个类at runtime//运行时. The page code refers to the code-behind file in theCodeFile attribute //代码文件属性of the <%@ Page %> directive, specifying the class name//类名称 in the Inherits attribute//继承属性. Note that members of the code behind class //代码分离类must be either public or protected //不是...就是...(they cannot be private). C# CodeBehind Code Separation<两个文件>
CodeBehind_cs.aspx
<%@ page language="C#" CodeFile="CodeBehind_cs.aspx.cs" Inherits="CodeBehind_cs_aspx" %>

<html>
<head>
<title>ASP.NET CodeBehind Pages</title>
</head>
<body>
<form runat="server">
<h1>Welcome to ASP.NET 2.0!</h1>
<b>Enter Your Name:</b>
<asp:TextBox ID="TextBox1" Runat="server"/>
<asp:Button ID="Button1" Text="Click Me" OnClick="Button1_Click" Runat="server"/>
<br />
<br />
<asp:Label ID="Label1" Text="Hello" Runat="server" />
</form>
</body>
</html>
CodeBehind_cs.aspx.cs
//-----------------------------------------------------------------------
// This file is part of the Microsoft .NET SDK Code Samples.
//
// Copyright (C) Microsoft Corporation. All rights reserved.
//
//This source code is intended only as a supplement to Microsoft
//Development Tools and/or on-line documentation. See these other
//materials for detailed information regarding Microsoft code samples.
//
//THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY
//KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
//IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
//PARTICULAR PURPOSE.
//-----------------------------------------------------------------------
using System;

public partial class CodeBehind_cs_aspx : System.Web.UI.Page
{
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = "Hello " + TextBox1.Text;
}

}
CodeBehind_cs.aspx
<%@ page language="C#" CodeFile="CodeBehind_cs.aspx.cs" Inherits="CodeBehind_cs_aspx" %>
<html>
<head>
<title>ASP.NET CodeBehind Pages</title>
</head>
<body>
<form runat="server">
<h1>Welcome to ASP.NET 2.0!</h1>
<b>Enter Your Name:</b>
<asp:TextBox ID="TextBox1" Runat="server"/>
<asp:Button ID="Button1" Text="Click Me" OnClick="Button1_Click" Runat="server"/>
<br />
<br />
<asp:Label ID="Label1" Text="Hello" Runat="server" />
</form>
</body>
</html>
CodeBehind_cs.aspx.cs
//-----------------------------------------------------------------------
// This file is part of the Microsoft .NET SDK Code Samples.
//
// Copyright (C) Microsoft Corporation. All rights reserved.
//
//This source code is intended only as a supplement to Microsoft
//Development Tools and/or on-line documentation. See these other
//materials for detailed information regarding Microsoft code samples.
//
//THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY
//KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
//IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
//PARTICULAR PURPOSE.
//-----------------------------------------------------------------------
using System;
public partial class CodeBehind_cs_aspx : System.Web.UI.Page
{
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = "Hello " + TextBox1.Text;
}
}
The advantage of the simplified //简单化的code-behind model //代码分离模型over//比 previous versions is that you do not need to maintain //维护separate declarations//声明 of server control variables//服务器端控件变量 in the code-behind class.//代码分离类 Using partial classes //部分类(new in 2.0) allows the server control IDs of the ASPX page to be accessed directly//被直接访问 in the code-behind file//代码分离页面. This greatly simplifies//简化 the maintenance//维护 of code-behind pages//代码分离页面.

浙公网安备 33010602011771号