The Single-File Model                                                        

With the single-file model, .NET source code is embedded within the HTML markup of the Web page. All control
declarations and their property settings are defined in the HTML mark up. All C# and VB code (declarations, event
handlers, properties, etc.) added to the page will be placed in a dedicated script section that only runs at the Web
server. All .NET code is removed from the page before it is sent to the Web browser. The code-behind model
(covered later) uses a separate code-behind file to hold the .NET code, although code (technically) can still be added
to the HTML markup file.

When using the single-file model, you gain a few benefits. Pages written using the single-file model are easier to
deploy, easier pass off to another developer and easier to add to another Web application. Because there is no
inheritance dependency between files, a single-file page is also much easier to rename. Managing files in a Source
Control Manager (SCM) such as Visual SourceSafe (VSS), Team Foundation Server (TFS) or Subversion is easier
since all file interaction take place on a single file. For C# developers, all Page events are exposed through
IntelliSense in the server-side script section.
These events are not available via IntelliSense in the code-behind
model!

To illustrate the single-file model, assume you have just created a new ASP.NET Web Site using Visual Studio 2008.
Simply right-click the
Web Site in the Solution Explorer window and click Add New Item...
This brings up this dialog box:























Here, you can:
     •        Set the name of the file.
     •        Pick the .NET language of your choice.
     •        Uncheck the “
Place code in separate file” box, so the .NET code will be embedded in the HMTL
              markup.
     •        Leave the “
Select master page” checkbox unchecked, which you will learn about later.

Looking at the designer window, you can see that there are actually three different views (tabs) of the same file:
Design, Split, and Source.






































Occasionally, a message appears at the splitter bar reminding you to that the two views are out of sync. Just click
on the reminder and it automatically synchronizes both views (panes) of the file.

Assume you wish to update the *.aspx file to display data in a GridView control via a server side <script> block.  
Here is the completed *.aspx content:


<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data.SqlClient" %>


<script runat="server">
protected void
btnFillData_Click(object sender, EventArgs e)
{
 SqlConnection sqlConn = new SqlConnection
   ("Data Source=.;Database=Cars;integrated security=true");
 SqlCommand cmd =
   new SqlCommand("SELECT * FROM Inventory", sqlConn);
 sqlConn.Open();
 SqlDataReader dr = cmd.ExecuteReader();
 carsGridView.DataSource = dr;
 carsGridView.DataBind();
 dr.Close();
 sqlConn.Close();
}
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
 <title>Untitled Page</title>
</head>
<body>
 <form id="form1" runat="server">
 <div>
 <asp:Label ID="lblInfo" runat="server"
   Text="Click on the Button to Fill the Grid">
 </asp:Label>
 <asp:GridView ID="carsGridView" runat="server">
 </asp:GridView>

 <asp:Button ID="btnFillData" runat="server"
     
 OnClick="btnFillData_Click" Text="Fill Grid" />
 </div>
 </form>
</body>
</html>



Please note that you will learn about ADO.NET in an upcoming chapter.

At the top, notice the simple Page directive, specifying C# as the language used by the server side <script> section.


<%@ Page Language="C#" %>


Also notice the Import directive at the top of the page:


<%@ Import Namespace="System.Data.SqlClient" %>


This directive allows you to specify the name of a.NET namespace you wish to use in a script block within the
page. Note that this "
Import" directive is only understood within an *.aspx file and is not language-specific. Do not
confuse this declaration with the language-specific keywords – “
using” in C# and “Imports” in VB. We will discuss
directives later in the next section.

The next point of interest is the declaration of the ASP .NET Web controls (the Label, GridView and Button).
ASP .NET Web controls are defined within the <form> tags. Every opening <asp> tag is closed with a
corresponding </asp> tag or a self-closing “/ >” tag.


<asp:Label ID="lblInfo" runat="server"
 Text="Click on the Button to Fill the Grid">
</asp:Label>

<asp:GridView ID="carsGridView"
runat="server">
</asp:GridView>

<asp:Button ID="btnFillData"
runat="server"
 OnClick="btnFillData_Click" Text="Fill Grid" />



Note that each ASP .NET Web control is marked with the runat="server" attribute. This informs the ASP .NET
runtime engine to process this control on the server in order to produce raw HTML for the client side browser. The
association between the clicking of the button and the custom
btnFillData_Click() method is done via the
OnClick event attribute. A series of name / value pairs are used to instruct the Web Form control how to render
itself, which can also be set in the
Properties window.

Finally we have a <script> tag, which is not holding script at all, but rather C# code.


<script runat="server">
 protected void btnFillData_Click(object sender, EventArgs e)
 {
   SqlConnection sqlConn = new SqlConnection
     ("Data Source=.;Database=Cars;integrated security=true");
   SqlCommand cmd = new SqlCommand("SELECT * FROM Inventory", sqlConn);
   sqlConn.Open();
   SqlDataReader dr = cmd.ExecuteReader();
   carsGridView.DataSource = dr;
   carsGridView.DataBind();
   dr.Close();
   sqlConn.Close();
 }
</script>  
 

     
The corresponding VB code block would look like so:


<script runat="server">
Sub btnFillData_Click(ByVal sender as Object, ByVal e as EventArgs)
 Dim sqlConn As New _
   SqlConnection("Data Source=.;Database=Cars;integrated security=true")
 Dim cmd As SqlCommand = New SqlCommand("SELECT * FROM Inventory", sqlConn)
 sqlConn.Open()
 Dim dr As SqlDataReader = cmd.ExecuteReader()
 carsGridView.DataSource = dr
 carsGridView.DataBind()
 dr.Close()
 sqlConn.Close()
End Sub
The Single File Model
Table of Contents
Copyright (c) 2008.  Intertech, Inc. All Rights Reserved.  This information is to be used exclusively as an
online learning aid.  Any attempts to copy, reproduce, or use for training is strictly prohibited.
Courseware
Training Resources
Tutorials
Services