Anda di halaman 1dari 4

Inline (Single File) vs.

CodeBehind
By Neil de Weerdt | 2 Sep 2004
.NET1.0.NET1.1VS.NET2003C#ASP.NETWindowsDevIntermediate

There are a few differences in the processing of code-behind and single-file pages. See Also
More like this More by this author

12
Article Browse Code Stats Revisions

3.59 (10 votes)

Sponsored Links

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.NETcode-behind model, you might find yourself working with singlefile 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-behindWeb Forms pages. There are a few differences in the processing of code-behind and single-file pages. Code Behind Single File The code is in <script> blocks in the The HTML and controls are in the .aspx file, and same .aspx file that contains the the code is in a separate .aspx.vb or.aspx.cs file. HTML and controls. The .aspx file derives from The code for the page is compiled into a separate the Pageclass. class from which the .aspx file derives. When the page is deployed, the All project class files (without the .aspx file itself) sourcecode is deployed along with are compiled into a .dll file, which is deployed to the the Web Forms page, because it is server without any sourcecode. When a request for physically in the .aspx file. the page is received, then an instance of the However, you do not see the code, project .dllfile is created and executed. 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 dont 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. 2. 3. 4. 5. 6. 7. Open an existing project, or create a new ASP.NET Web application. On the Project menu, click Add HTML Page. Name the new page with the .aspx extension, for example, SingleForm1.aspx. Design the form. When your form works in code behind now, we can move it to a Single File. Change your design view to HTML. Replace your Page directive with:
Collapse | Copy Code

<%@ Page language="c#" %>

8. Between the <Head></Head> tags, add the following code:


Collapse | Copy 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.
Collapse | Copy Code

<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.
Collapse | Copy Code

<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:
Collapse | Copy 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 dont include the namespace. You only declare the class:
Collapse | Copy Code

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.
Collapse | Copy Code

<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

Anda mungkin juga menyukai