Skip to Main Content
Programming ASP.NET 3.5, 4th Edition
book

Programming ASP.NET 3.5, 4th Edition

by Dan Maharry, Dan Hurwitz, Jesse Liberty
October 2008
Intermediate to advanced content levelIntermediate to advanced
1166 pages
28h 31m
English
O'Reilly Media, Inc.
Content preview from Programming ASP.NET 3.5, 4th Edition

The Code-Behind File

You can interweave content such as HTML, text, server controls, and program code in a single file, as was done with traditional ASP. This is known, cleverly, as the single-file model.

To see an example of the single-file model, look at CodeSingleFile.aspx, listed in Example 6-1.

Example 6-1. CodeSingleFile.aspx

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
   protected void btnHello_Click(object sender, EventArgs e)
   {
      lblMessage.Text = "Hello. The time is " +
         DateTime.Now.ToLongTimeString();
   }
</script>

<html>
<head runat="server">
   <title>Single File Demo</title>
</head>
<body>
   <form id="form1" runat="server">
   <div>
      <h1>Code-Beside</h1>
      <asp:Button ID="btnHello" runat="server"
         Text="Hello" OnClick="btnHello_Click" />
      <br />
      <asp:Label ID="lblMessage" runat="server" />
   </div>
   </form>
</body>
</html>

If you place this file in a folder on your machine (such as c:\websites), create a virtual directory in Internet Information Services (IIS) pointing to that folder (named, for example, Websites), and then enter in your browser the URL http://localhost/websites/CodeSingleFile.aspx, the page will appear, containing a button that will display a text string every time it is clicked.

Tip

IntelliSense is available as you write code blocks, HTML, and server controls. New in VS2008, IntelliSense is also available for Cascading Style Sheets (CSS) and JavaScript. ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Programming Microsoft® ASP.NET 3.5

Programming Microsoft® ASP.NET 3.5

Dino Esposito
Learning ASP.NET 3.5, 2nd Edition

Learning ASP.NET 3.5, 2nd Edition

Brian MacDonald, Dan Hurwitz, Jesse Liberty
Pro ASP.Net 4 in C# 2010

Pro ASP.Net 4 in C# 2010

Matthew MacDonald, Adam Freeman, Mario Szpuszta
Programming .NET 3.5

Programming .NET 3.5

Jesse Liberty, Alex Horovitz

Publisher Resources

ISBN: 9780596156657Supplemental ContentErrata Page