大网数据广告活动
Web服务器与PHP

ASP.NET Code-behind model overview

· 52CE 我爱测

This article provides information about the code-behind model that is introduced in ASP.NET.

This article provides a brief overview of the in-line code and Code-behind model in ASP.NET.

Original product version:   ASP.NET
Original KB number:   303247

Summary

This article refers to the Microsoft .NET Framework class library namespace System.Web.UI.

ASP.NET supports two methods to author pages:

  • In-line code
  • Code-behind

In-Line code

In-line code is code that is embedded directly within the ASP.NET page. The following code represents a sample ASP.NET page that includes in-line code:

Myinlinecode.aspx

<%@ Language=C# %>
<HTML>
    <script runat="server" language="C#">
        void MyButton_OnClick(Object sender, EventArgs e)
        {
            MyLabel.Text = MyTextbox.Text.ToString();
        }
    </script>
    <body>
        <form id="MyForm" runat="server">
            <asp:textbox id="MyTextbox" text="Hello World" runat="server"></asp:textbox>
            <asp:button id="MyButton" text="Echo Input" OnClick="MyButton_OnClick" runat="server"></asp:button>
            <asp:label id="MyLabel" runat="server"></asp:label>
        </form>
    </body>
</HTML>

Code-behind

Code-behind refers to code for your ASP.NET page that is contained within a separate class file. This allows a clean separation of your HTML from your presentation logic. The following sample illustrates an ASP.NET code-behind page:

  • MyCodebehind.aspx

    <%@ Language="C#" Inherits="MyStuff.MyClass" %>
    <HTML>
        <body>
            <form id="MyForm" runat="server">
                <asp:textbox id="MyTextBox" text="Hello World" runat="server"></asp:textbox>
                <asp:button id="MyButton" text="Echo Input" Onclick="MyButton_Click" runat="server"></asp:button>
                <asp:label id="MyLabel" runat="server" />
            </form>
        </body>
    </HTML>
    
  • Mycodebehind.cs

    using System;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    namespace MyStuff
    {
        public class MyClass : Page
        {
            protected System.Web.UI.WebControls.Label MyLabel;
            protected System.Web.UI.WebControls.Button MyButton;
            protected System.Web.UI.WebControls.TextBox MyTextBox;
    
            public void MyButton_Click(Object sender, EventArgs e)
            {
                MyLabel.Text = MyTextBox.Text.ToString();
            }
        }
    }
    

In the preceding sample, you can use the following syntax to compile Mycodebehind.cs:

csc.exe /out:mycodebehind.dll /t:library mycodebehind.cs

When you use the following code, the code-behind page inherits from the Page class. The Page class resides in the System.Web.UI namespace:

public class MyClass : Page

Inheriting from the Page class gives the code-behind page access to the ASP.NET intrinsic objects, such as Request and Response. In addition, inheriting from the Page class provides a framework for handling events for controls within the ASP.NET page.

In the preceding sample, the code-behind page is compiled before ASP.NET runs. Alternatively, you can reference the code-behind class by using an SRC tag as follows:

<%@ Language="C#" Inherits="MyStuff.MyClass" src="MyCodebehind.cs" %>

In this case, ASP.NET compiles the code-behind page on the fly. This compilation step only occurs when the code-behind file is updated (which is detected through a timestamp change).

Code-behind support in Visual Studio .NET

When you use Visual Studio .NET to create ASP.NET Web Forms, code-behind pages are the default method. In addition, Visual Studio .NET automatically performs pre-compilation for you when you build your solution.

Note: Code-behind pages that are created in Visual Studio .NET include a special page attribute, Code-behind, which Visual Studio .NET uses.

Source: Microsoft Learn / IT Pro troubleshooting by Microsoft and contributors. Documentation licensed CC BY 4.0. Code samples are covered by the MIT License. Converted from Markdown, with links normalized and embedded media omitted.

资料来源与适用范围

资料来源
Microsoft Learn / IT Pro troubleshooting
适用范围
See source applicability · Source date 04/02/2020
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY 4.0 (documentation), MIT (code samples)

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。