Page Structure in ASP.Net

Today I describe the ASP.Net page structure. Which page structure you need to use and what are the differences between them. There are two ways structuring your code in an ASP.Net page. These two ways are:

  1. Code-Inline Model
  2. Code-Behind Model

First Option is to use Code-Inline Model

Let us start with the Code-Inline mode. Most ASP.Net 2.0/3.0 developers are familiar with this model. In this model you need to write your code in a single .aspx page. When the developers were working with .Net Framework 1.0/1.1 they move out of their way and start building their ASP.Net pages in Inline-Code model. In Visual Studio you can simply select your page type to build ASP.Net page in Inline Code. For this you need to uncheck the "Place code in separate file" checkbox.


Here is the list of files for using inline code:
  • Web form
  • Ajax web form
  • Master pages
  • Ajax master pages
  • Web user controls
  • Web services

So now for a little practical information.

Add a TextBox and a button to your page. Now double-click on the button. Now write the following code in the button click:

  1. protected void Button1_Click(object sender, EventArgs e)  
  2. {  
  3.     Response.Write("Hii my self "+TextBox1.Text+" I love .net and i am crazy for it");  
  4. }

Run your page and check the results.

The Second Option you can use is a Code-Behind Model

Now suppose you are working with an inline-model and your code has more than 400-500 lines. In the same page you have the UI of the page. Then how complex is your page now? Do you think about it? So to resolve the complexity issue of a aspx page you can use the Code-Behind Model. For this you need to check the checkbox "Place code in separate file". See in the Solution Explorer, you will see a separate file has been created with a .cs extension.
Now you can do the same on this page and notice that the code that you write after a double-click on a button is in another file.
Code-Behind just separates your business logic and presentation logic. It is my recommendation to use the Code-Behind Model.

I just want to add something extra. Notice in your aspx file, there are some attributes in the page directive, as in the following:
First is "Codefile", this attribute is for your .cs file. It points to the cs file of your aspx page. The second attribute is "Inherit". It specifies the name of the class that is bound to the page when the page is compiled.

Last words

So the summary is this, that there are two types of page structures available in ASP.Net. One is code behind with a separate file for the code and another is the Inline-Model for working in a single file. The final decision of what to use is for you to make. I hope you learn something from here. Provide your valuable comments after reading this article. Your comments are important for me. It is really important for me to know what you think about this article. I hope you like my article. Your support is important to me.