![]() |
Web Development »
ASP.NET »
General
Intermediate
Inline (Single File) vs. CodeBehindBy Neil de WeerdtThere are a few differences in the processing of code-behind and single-file pages. |
C#, Windows, .NET 1.0, .NET 1.1, ASP.NET, VS.NET2003, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
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 Behind | Single 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]
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.
Page directive with: <%@ Page language="c#" %>
<Head></Head> tags, add the following code: <Head> <script language="CS" runat="server"></script> </Head>
<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>
InitializeComponent method, I registered my events in the Page_Load.
<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>
<Head></Head> tags, add the following code: <Head> <script language="cs" runat="server" src= "MySource.cs"/> <script language="CS" runat="server"></script> </Head>
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;
}
}
}
<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>
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 2 Sep 2004 Editor: Smitha Vijayan |
Copyright 2004 by Neil de Weerdt Everything else Copyright © CodeProject, 1999-2009 Web10 | Advertise on the Code Project |