Code behind sample in Asp .Net

   This article explains how to use code behind model while programming in asp .net. This feature in asp .net enables us to separate the user interface elements from the application logic. The user interface or GUI tags are kept in the .aspx page and the business logic or functional code can become a part of pagename.aspx.cs files. 

   It is not widely discussed but this model could lead to a slight performance gain (though not very significant) when compared to putting the code in the same .aspx file. But in any case the code in the .cs (code behind) file is going to be a compiled version, with Internet Information Server looking for a dll file for rendering the logic/pages.

Creating the ASP .Net Application with Microsoft Visual Studio .Net:

 The project can be created in the Visual Studio .net by choosing an ASP .Net Application inside the C# projects option. When a new project is created in such a way, the VS .Net automatically connects to the IIS, Creates a virtual directory along with a project directory. After this, it compiles this c-sharp files into a .dll and puts the dll in the ProjectPathbin folder. Now when the page is accessed via the Web browser, the ASP .Net run-time checks the bin folder for the run time dll and executes the page accordingly. 

   The same example used in the article has been used and given as a project attachment. Download the sample by clicking this Sample Project link.

Creating the  ASP .Net Application by compilation in Command Line:

   For those who want to create the dll without the Visual Studio .Net can create it on the command line by using the command as follows. This can be used as an alternative for those who do not have or do not want to use MS VS .Net.

    csc /target:library /out:.bin/mylib.dll /recurse:*.cs

   The above command recursively compiles all the associated .cs c# files in the folder and copies the dll to the bin folder. As usual when the browser sends a request for the aspx page, the IIS automatically executes the aspx page and the associated compiled code from the dll. One important point to be kept in mind is, if an asp .net application is created in command line compilation mode, the folder must be configured as a virtual directory in the IIS Inetmgr.exe tool. Otherwise the page cannot be rendered and IIS returns an error. If Servers like Webmatrix or Cassini are used, then this will not be a problem. The folder need not be configured as a virtual directory.

   One important fact to be noted in this model is even though the code is compiled as a dll from either a c# or vb .net code, the code will always run in the context of ASP .Net. This prevents malicious access to the local computer, which can be destructively used by spammers.

   A very definitely pleasant thing about using Visual studio .net while writing any application is intellisense. There has been a very major improvement in the features of intellisense. It makes the application development much much faster than it used to be in the previous versions of Visual Studio (non .net versions).

Note: Another important point to be noted is if the project is just created and if the page is accessed without compiling the project, the server will return an error. This is because the libraries with the associated namespaces are not yet built. So it is very important to build the project either from the Visual studio .net menu or Ctrl + Shift + B short cut keys in MS VS .Net or through the command line.