Microsoft Knowledge Base Email Alertz

This article provides a brief overview of the code-behind model, which is introduced in ASP.NET.

Search KbAlertz

Advanced Search

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]











Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks

Article ID: 303247 - Last Review: February 23, 2007 - Revision: 3.4

INFO: ASP.NET Code-Behind Model Overview

This article was previously published under Q303247
This article refers to the following Microsoft .NET Framework Class Library namespace:
  • System.Web.UI

On This Page

SUMMARY

This article provides a brief overview of the code-behind model, which is introduced in ASP.NET.

MORE INFORMATION

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. Notice that 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 Microsoft Visual Studio .NET to create ASP.NET Web Forms, code-behind pages are the default method. In addition, Visual Studio .NET automatically performs precompilation for you when you build your solution. Note that code-behind pages that are created in Visual Studio .NET include a special page attribute, Codebehind, which Visual Studio .NET uses.

REFERENCES

For additional information%1, click the article number%2 below to view the article%2 in the Microsoft Knowledge Base:
313105  (http://kbalertz.com/Feedback.aspx?kbNumber=313105/EN-US/ ) BUG: Cannot Compile Code-Behind Files That Use Src Attribute on a UNC Share

APPLIES TO
  • Microsoft ASP.NET 1.1
  • Microsoft ASP.NET 1.0
Keywords: 
kbinfo kbwebforms KB303247
       

Community Feedback System

Very often, it takes hours to solve a problem. Very often, you've looked high and low, and have tried a lot of solutions. When you finally found it, chances are, it was because someone else helped you. Here's your chance to give back. Use our community feedback tool to let others know what worked for you and what didn't.

Please also understand that the community feedback system is not warranted to be correct, it's simply a system that we've built to let people try and help each other. If something in a feedback response doesn't make sense to you, or you're not comfortable making changes that the feedback talks about (like registry edits), please consult a professional.

Thank you for using kbAlertz.com Feedback System.

-- Scott Cate