Read Build Your Own ASP.NET 3.5 Website Using C# & VB Online
Authors: Cristian Darie,Zak Ruvalcaba,Wyatt Barnett
Tags: #C♯ (Computer program language), #Active server pages, #Programming Languages, #C#, #Web Page Design, #Computers, #Web site development, #internet programming, #General, #C? (Computer program language), #Internet, #Visual BASIC, #Microsoft Visual BASIC, #Application Development, #Microsoft .NET Framework
creating “spaghetti” code—snippets of code that were scattered throughout the
presentation elements. This made it very tricky to manage the code between development teams, as you’ll know if you’ve ever tried to pick apart someone else’s ASP
code. In response to these problems, ASP.NET introduced a new development approach that allows code developers to work separately from the presentation designers who lay out individual pages. This new approach, called
code-behind
, keeps all of your presentational elements
(controls) inside the
.aspx
file, but moves all of your code to a separate class in a
.vb
or
.cs
code-behind file. Consider the following ASP.NET page, which displays a
simple button and label:
Visual Basic
LearningASP\VB\HelloWorld.aspx
<%@ Page Language="VB" %>
Licensed to [email protected]
VB and C# Programming Basics
87
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
C#
LearningASP\CS\HelloWorld.aspx
<%@ Page Language="C#" %>
⋮
⋮
Let’s see how this example could be separated into the following distinct files:
HelloWorldCodeBehind.aspx
layout, presentation, and static content
HelloWorldCodeBehind.aspx.vb or HelloWorldCodeBehind.aspx.cs
code-behind files containing a custom page class
Licensed to [email protected]
88
Build Your Own ASP.NET 3.5 Web Site Using C# & VB
Since there isn’t a lot of code to type, you could create these files with any text editor, including Notepad. Visual Web Developer makes things easier for you, though. When adding a new Web Form file to the project, you have the option—which
you’ve already noticed when creating new pages—to
Place code in separate file
.
Create a new Web Form for your project by clicking
Website > Add New Item…
and
choosing the
Web Form
template. Check the
Place code in a separate file
checkbox, type
HelloWorldCodeBehind.aspx
for the file name, and click
Add
. The default code Visual Web Developer generates for the Web Form and its code-behind file is very
similar to the code of the
Default.aspx
form it created for your new Web Site project, back in
Chapter 1
.
We’ll start with the ASP.NET Web Form file
HelloWorldCodeBehind.aspx
. All we
have to do is change the page title and insert the ASP.NET controls—a Button and
a Label:
Visual Basic
LearningASP\VB\HelloWorldCodeBehind.aspx
(excerpt)
<%@ Page Language="VB" AutoEventWireup="false"
CodeFile="HelloWorldCodeBehind.aspx.vb"
Inherits="HelloWorldCodeBehind" %>
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
Licensed to [email protected]
VB and C# Programming Basics
89
C#
LearningASP\CS\HelloWorldCodeBehind.aspx
(excerpt)
<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="HelloWorldCodeBehind.aspx.cs"
Inherits="HelloWorldCodeBehind" %>
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
As you can see, without code blocks, the main ASP.NET page becomes a bit simpler.
You might also notice that the only line that differs between these
.aspx
pages is the Page directive. Since the
.aspx
pages now contain only HTML layout, the contents
are identical no matter what language you use for the code.
You’ll also notice that the code-behind file (
HelloWorldCodeBehind.aspx.vb
or
Hello-
WorldCodeBehind.aspx.vb
) has been generated automatically, as it was for the
De-
fault.aspx
file back in Chapter 1. This is a pure code file, and contains no HTML or
other markup tags. Nevertheless, we can still access presentation elements from
this file, using their IDs (such as messageLabel).
Change the code to look like this:
Visual Basic
LearningASP\VB\HelloWorldCodeBehind.aspx.vb
Imports System
Imports System.Web.UI
Imports System.Web.UI.WebControls
Licensed to [email protected]
90
Build Your Own ASP.NET 3.5 Web Site Using C# & VB
Partial Class HelloWorldCodeBehind
Inherits System.Web.UI.Page
Sub Click(ByVal s As Object, ByVal e As EventArgs)
messageLabel.Text = "Hello World!"
End Sub
End Class
C#
LearningASP\CS\HelloWorldCodeBehind.aspx.cs
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class HelloWorldCodeBehind : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
public void Click(Object s, EventArgs e)
{
messageLabel.Text = "Hello World!";
}
}
The code in the code-behind file is written differently than the code we’ve written
using