Click here to Skip to main content
15,888,610 members
Articles / Web Development / ASP.NET
Article

Inline (Single File) vs. CodeBehind

Rate me:
Please Sign up or sign in to vote.
4.15/5 (14 votes)
2 Sep 20043 min read 255.4K   31   12
There are a few differences in the processing of code-behind and single-file pages.

Introduction

Well, to start, I used a lot of Quotes from MSDN. I think they can explain things better than me, so let's start.

Although Microsoft® Visual Studio® .NET makes it easy to create and work with Web Forms pages using the ASP.NET code-behind model, you might find yourself working with single-file Web Forms pages by circumstance or by preference. This article gives an overview of the differences between the two models, describes how to work with single-file Web Forms pages in Visual Studio, and shows you how to convert single-file .aspx pages to code-behind Web Forms pages.

There are a few differences in the processing of code-behind and single-file pages.

Code BehindSingle File
The HTML and controls are in the .aspx file, and the code is in a separate .aspx.vb or .aspx.cs file.The code is in <script> blocks in the same .aspx file that contains the HTML and controls.
The code for the page is compiled into a separate class from which the .aspx file derives.The .aspx file derives from the Page class.
All project class files (without the .aspx file itself) are compiled into a .dll file, which is deployed to the server without any source code. When a request for the page is received, then an instance of the project .dll file is created and executed.When the page is deployed, the source code is deployed along with the Web Forms page, because it is physically in the .aspx file. However, you do not see the code, only the results are rendered when the page runs.

[Quote MSDN: Working with Single-File Web Forms Pages in Visual Studio .NET]

Using the code

My personal preference is Code Behind. Most free ASP.NET hosting servers don’t allow Code Behind, not sure why, yet.

What I sometimes do is I write a base class, which derives from Page class, and all my pages derive from my base class. But on a Single File Web Form, it derives from the Page class. So, this limits you to create your own base derived class.

Let's start off with a simple app that uses Code Behind and convert that into Single File.

  1. Open an existing project, or create a new ASP.NET Web application.
  2. On the Project menu, click Add HTML Page.
  3. Name the new page with the .aspx extension, for example, SingleForm1.aspx.
  4. Design the form.
  5. When your form works in code behind now, we can move it to a Single File.
  6. Change your design view to HTML.
  7. Replace your Page directive with:
    <%@ Page language="c#" %>
  8. Between the <Head></Head> tags, add the following code:
    <Head>
       <script language="CS" runat="server"></script>
    </Head>
  9. Copy and paste your Code Behind code in between the <script> tags.

    Note that I don't have any private / public / ... modifier.

    <Head>
      <script language="CS" runat="server">
        void Page_Load(object sender, System.EventArgs e) 
        {
          // Put user code to initialize the page here 
        } 
        void btnLogon_Click(object sender, System.EventArgs e) 
        { 
          this.txtUserID.Text = "Logon"; 
          this.txtPassword.Text = ""; 
        }
      </script>
    </Head>
  10. Now. we need to “Register” the event. Due to the fact the I could find the InitializeComponent method, I registered my events in the Page_Load.

    C#
    <Head>
     <script language="CS" runat="server">
       void Page_Load(object sender, System.EventArgs e) 
       { 
         // Put user code to initialize the page here
             this.btnLogon.Click +=  
               new System.EventHandler(this.btnLogon_Click);
       } 
       void btnLogon_Click(object sender, System.EventArgs e) 
       { 
         this.txtUserID.Text = "Logon";
         this.txtPassword.Text = "";
       }
     </script>
    </Head>
  11. There we go.

Now how do I use classes in inline code???

  1. Well to start off, you can create a .cs file where your class code is declared.
  2. Between the <Head></Head> tags, add the following code:
    <Head>
        <script language="cs" runat="server" src= "MySource.cs"/>
      <script language="CS" runat="server"></script>
    </Head>
  3. Note that in your source file, you don’t include the namespace. You only declare the class:
    C#
     public class MyClass1 { }
     public class MyClass2 
     { 
        protected int Index = 0; 
        public MyClass2()
        { 
        } 
     } 
     public class MyClass3 : System.Collections.CollectionBase
     { 
        public int this[int Index] 
        { 
            get 
            { 
                return (int)List[Index];
            } 
            set 
            { 
                List[Index] = value;
            } 
        } 
    }
  4. And you use it the same as what you would have in code behind.
    C#
    <Head>
        <script language="CS" runat="server">
            void Page_Load(object sender, System.EventArgs e)
            { 
                // Put user code to initialize the page here
                     this.btnLogon.Click += 
                  new System.EventHandler(this.btnLogon_Click);
                MyClass3 class3 = new MyClass3();
                class[0] = "Test";
            } 
            void btnLogon_Click(object sender, System.EventArgs e)
            { 
                this.txtUserID.Text = "Logon"; 
                this.txtPassword.Text = "";
            }
        </script>
    </Head>

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
South Africa South Africa
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questiontool for converting inline to codebhind and vs Pin
Hillerstrom Consulting24-Jan-06 21:54
Hillerstrom Consulting24-Jan-06 21:54 
AnswerRe: tool for converting inline to codebhind and vs Pin
Neil de Weerdt26-Jan-06 19:56
Neil de Weerdt26-Jan-06 19:56 
GeneralRe: tool for converting inline to codebhind and vs Pin
jsllvn3-Feb-06 6:17
jsllvn3-Feb-06 6:17 
Generaluse both inline code and code-behind code simultaneously - even in different langauages Pin
Member 159673417-Dec-04 2:08
Member 159673417-Dec-04 2:08 
GeneralRe: use both inline code and code-behind code simultaneously - even in different langauages Pin
Neil de Weerdt26-Jan-06 20:07
Neil de Weerdt26-Jan-06 20:07 
GeneralMore discussion....please Pin
JOlson10-Sep-04 7:38
JOlson10-Sep-04 7:38 
GeneralNew Model in asp.net 2.0 Pin
Karl Seguin8-Sep-04 14:48
Karl Seguin8-Sep-04 14:48 
Generalcode behind without dll Pin
Ashley van Gerven4-Sep-04 0:52
Ashley van Gerven4-Sep-04 0:52 
GeneralRe: code behind without dll Pin
Kastellanos Nikos8-Sep-04 3:59
Kastellanos Nikos8-Sep-04 3:59 
GeneralRe: code behind without dll Pin
Alberto Venditti8-Sep-04 22:46
Alberto Venditti8-Sep-04 22:46 
Probably, Ashley van Gerven refers to the use of "SRC" attribute in the @Page directive; it allows you to avoid the explicit build of the code-behind DLL, causing an automatic on-the-fly compilation of each code-behind in a temporary DLL.

GeneralRe: code behind without dll Pin
Kastellanos Nikos9-Sep-04 0:13
Kastellanos Nikos9-Sep-04 0:13 
GeneralRe: code behind without dll Pin
Neil de Weerdt9-Sep-04 1:20
Neil de Weerdt9-Sep-04 1:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.