Sunday, March 7, 2010

ASP.NET Coding Model- Inline Code, Code-Behind & Code-Beside

Inline Code:
It’s an old style of coding model, where your business logic code appears in
< script runat=”server”> < /script > block in the .aspx page itself.

Example:

< %@ Page Language=”C#” % >
< script runat=”server”>
void Page_Load(object sender, eventArgs e)
{
Label1.Text=”Hello World”;
}
< /script>
< html >
< body >
< form id=”form1” runat=”server” >
< asp:Label ID=”Label1” Runat=”server” / >
< /form >
< /body >
< /html >

When first time this page loads,it is compiled into a class that resides in an assembly stored in a subfolder of the “C:\WINDOWS\Microsoft.NET\Framework\v2.x.xxxx\Temporary AST.NET files” folder. This compiled version of the page is good until the .aspx file changes or the application is restarted, at that time it will have to be recompiled.
Note- By default web page inherits from the System.Web.UI.Page class, so if you want your page inherits from some other class (which in turn must inherit from Page class), add “inherits” attribute to the Page directive.

Code-Behind: Non-Compiled

This coding style was used by the people, who wanted to change code on-the-fly without having to build their projects, or who wanted to separate code from HTML but didn’t have Visual Studio tool.

Example:

default.aspx file:

< %@ Page Language=”C#” Src=”default.aspx.cs” Inherits=”CodeBehindClass” % >
< html >
< body >
< form id=”form1” runat=”server” >
< asp:Label ID=”Label1” Runat=”server” / >
< /form >
< /body >
< /html >

Default.aspx.cs file:

using System;
using System.Web;
public class CodeBehindClass : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label Label1;
public void Page_Load(object sender, eventArgs e)
{
Label1.Text=”Hello World”;
}
}

In the above cod, the .aspx page looks just like the inline page, except the code has been moved to the code-behind file and two more attributes has been added to the Page directive. The Src attribute points to the physical file where the code behind class is located. The Inherits attributes tells us which class in that code-behind file the page should inherit because there technically could be any number of classed in that file.
The code-behind class must declare all the controls on the page as protected member of the class.
When the first time this page is requested, it will be compiled and cached only as the inline code version, using the code-behind file as the source for its base class. It will remain cached until the .aspx or code-behind file has been changed or the application is restated.

Code-Behind: Compiled

This coding style is same as the previous one for the coding point of view except in the Page directive “Src” attribute is replaced with “Codebehind”.
e.g.-
< %@ Page Language=”C#” Codebehind=”default.aspx.cs” Inherits=”CodeBehindClass” % >

Rest of the code will be same as shown in the Code-Behind Non-Compiled coding style.
By the way this coding style was used only by Visual Studio 2002 and 2003. Codebehind attribute was used to let the designer know where the code resides. Because no Src was specified, the class indicated in the Inherits attribute had to be compiled in an assembly located in the /bin folder. Actually when the project was built, all the .cs (or .vb) files in the project were compiled into one assembly. When the first time page was requested, it would be compiled in much the same way as the previous method, except that it would inherit from a class compiled in an assembly found in the /bin folder.
This style is obsolete and not supported in Visual Studio 2005 as it required constant synchronization between the page and code files that would generate lots of code. It also required a build every time page is tested.

Code-Beside:

This coding style uses the concept of partial class to separate the code files from the .aspx page.

Example:

default.aspx file:

< %@ Page Language=”C#” Codefile=”default.aspx.cs” Inherits=”Default_aspx” % >
< html >
< body >
< form id=”form1” runat=”server” >
< asp:Label ID=”Label1” Runat=”server” / >
< /form >
< /body >
< /html >

Default.aspx.cs file:

using System;
using System.Web;
public class Default_aspx: System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label Label1;
public void Page_Load(object sender, eventArgs e)
{
Label1.Text=”Hello World”;
}
}

When a request for the page is made for the first time, the page combines itself with the partial class to become the functional equivalent of the inline code. This compiled page remains valid until one of the involved files is changed.