HOME RESOURCES PLACEMENTS CONTACT FEEDBACK SUBSCRIBE
QUICK LINKS .NET Articles
.NET Training Tutorial
HR Interview Questions
.NET Interview Questions
SQL Interview Questions
JAVA Interview Questions

New CodeBehind model in ASP.NET 2.0
To understand the new code behind model in ASP.NET 2.0 create a page with a TextBox,Button and a Label as shown in figure1 below.

Figure1

The HTML for the page with the above layout is in the below code snippet. The "CodeFile" attribute highlighted in blue is used to specify "Default.aspx.cs" is the file that contains the server side code for this aspx page. Therefore in ASP.NET 2.0 we use the CodeFile attribute to associate an aspx page with its corressponding codebehind file.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<head runat="server">
     <title>PRAGIM</title>
</head>

<body style="font-size: 11pt; font-family: Arial">
   <form id="form1" runat="server"> Please enter your name : <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br /> <br />
                    <asp:Button ID="Button1" runat="server" Font-Bold="True" Font-Names="Arial" Font-Size="Medium" Text="Button" /><br />                   
                    <asp:Label ID="Label1" runat="server" Font-Bold="True" Font-Names="Arial" Font-Size="Medium"></asp:Label>
   </form>
</body>

Now right click on the above aspx page and select ViewCode option. The codebehind file that is generated is shown in the below code snippet. There are 2 changes that you see in the new code behind page.
  • Declarations for the TextBox,Button and Label control are missing from the code behind file: In ASP.NET 2.0 we will not have the control declarations in the code behind file. The control declarations are present in another sibling class that is generated by ASP.NET. Later we will findout where and how does this class look like. Though the condtrol declarations are not present in the CodeBehind class we will still have intellisense feature on these controls.
  • The code behind class is declared as a partial class: The reason the code behind class is declared as partial because, this class contains only the code that we write and does not include the code that is generated by VisualStudio. For example the control declarations code is not present. So the code behind class is only a part of the class. Visual Studio will generate another partial class automatically which contains control declarations and other system generated code. Later we will see where to look for this partial class that is automatically generated by Visual Studio.
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 Page_Load(object sender, EventArgs e)
     {
     } 
     protected void Button1_Click(object sender, EventArgs e)
     {
         Label1.Text = "Hello " + TextBox1.Text;
     }
}

Now let us find out where will the partial class generated by visual studio present. To find out that add the following code to your page_load evenhandler.
protected void Page_Load(object sender, EventArgs e)
{
    Response.Write(System.Reflection.Assembly.GetExecutingAssembly().Location);
}

After you have added the above code, run the application and you should see the path from where the files are executed as shown in the below figure2.

Figure 2

Copy the above path and navigate to the folder specified. There you should find 3 class files with .cs extension. If you donot find them, set the page level debug attribute to true. This will make the source code files to be present in the scratch directory. When I have naviagted to the above path, I found 3 source code files as shown in the below figure.

Figure 3

What are these 3 source code files and what do they contain:
  • 1 class is that ASP.NET 2.0 will generate as a result of parsing the .aspx file, and it looks similar to the file that is produced today in ASP.NET 1.1 from .aspx page parsing. Note that it derives from our code-behind class.
  • 1 class is our code behind class that we have written. Note that this class is declared as partial class.
  • The last one is the sibling partial class for our code behind class, that is generated by visual studio which contains system generated code .For example declarations for controls on the form is present in this partial class.
Points to remeber about the new code behind model:
  • CodeFile attribute is used to specify which .cs or .vb file is used as a code behind file for the aspx file.
  • In ASP.NET 2.0 code behind model, we need not have declarations for controls. Instead these declarations are present in a sibling partial class that is generated by visual studio.
  • The code behind class has to be declared as Partial class.

Njoy Programming
   ByPrasad Cherukuri