Visual Studio Web Applications—Code Model

Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio


Jump to: navigation, search
CSharp-Online.NET:Articles
Visual Studio Articles

Visual Studio Web Apps

© 2006 M. MacDonald, M. Szpuszta

The Code Model

So far, you’ve learned how to design simple web pages, and you’ve taken a tour of the Visual Studio interface. But before you get to serious coding, it’s important to understand a little more about the underpinnings of the ASP.NET code model. In this section, you’ll learn about your options for using code to program a web page and how ASP.NET events wire up to your code.

Visual Studio supports two models for coding web pages and web services:

Inline code: This model is the closest to traditional ASP. All the code and HTML is stored in a single .aspx file. The code is embedded in one or more script blocks. However, even though the code is in a script block, it doesn’t lose IntelliSense or debugging support, and it doesn’t need to be executed linearly (like classic ASP code). Instead, you’ll still react to control events and use subroutines. This model is handy because it keeps everything in one neat package, and it’s popular for coding simple web pages.
Code-behind: This model separates each ASP.NET web page into two files: an .aspx markup file with the HTML and control tags, and a .cs code file with the source code for the page. This model provides better organization, and separating the user interface from programmatic logic is keenly important when building complex pages. In Visual Studio 2005, the implementation of the code-behind model has changed, but the overall philosophy is the same.

In .NET 1.0 and 1.1, the design tool you choose determines the model you use. With Visual Studio, you have the freedom to use both approaches. When you add a new web page to your website (using Website > Add New Item), the Place Code in a Separate File check box chooses whether you want to use the code-behind model (see Figure 2-21). Visual Studio remembers your previous setting for the next time you add a new page, but it’s completely valid (albeit potentially confusing) to mix both styles of pages in the same application.


Image:7680f0221.jpg
Figure 2-21. Choosing the code model


To understand the difference, it helps to consider a simple page, like the following dynamic time example; this is TestFormInline.aspx, which shows how the page looks with inline code:

<%@ Page Language="C#" %>
<script runat="server">
    protected void Button1_Click(object sender, EventArgs e)
    {
        Label1.Text = "Current time: " + DateTime.Now.ToLongTimeString();
    }
</script>
 
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Test Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="Label1" runat="server" Text="Click Me!"></asp:Label><br />
        <br />
        <br />
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click"
         Text="Button" /></div>
    </form>
</body>
</html>

The following listings, TestFormCodeBehind.aspx and TestFormCodeBehind.aspx.cs, show how the page is broken up into two pieces using the code-behind model. This is TestFormCodeBehind.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="TestFormCodeBehind.aspx.cs"
    Inherits="TestFormCodeBehind"%>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Test Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="Label1" runat="server" Text="Click Me!"></asp:Label><br />
        <br />
        <br />
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click"
         Text="Button" /></div>
    </form>
</body>
</html>

This is TestFormCodeBehind.aspx.cs:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
 
public partial class TestFormCodeBehind : System.Web.UI.Page
{
    protected void Button1_Click(object sender, EventArgs e)
    {
        Label1.Text = "Current time: " + DateTime.Now.ToLongTimeString();
    }
}

The only real difference between the inline code example and the code-behind example is that the page class is no longer implicit in the latter—instead it’s declared to contain all the page methods.

Overall, the code-behind model is preferred for complex pages. Although the inline code model is slightly more compact for small pages, as your code and HTML grows it becomes much easier to deal with both portions separately. The code-behind model is also conceptually cleaner, as it explicitly indicates the class you’ve created and the namespaces you’ve imported. Finally, the code-behind model introduces the possibility that a web designer may refine the markup in your pages without touching your code. This book uses the code-behind model for all examples.


Previous_Page_.gif Next_Page_.gif




Personal tools