ASP.NET Controls Explained: Part 1/2

Code Behind

Now that we've covered user controls, HTML controls, and server controls, it's time to look at the code behind method, which you can use to further separate the presentation layer from the application logic layer. The code behind method is a lifesaver for developers planning to undertake large-scale web-based projects.

To use the code behind methods, we need to create two separate files: the first is a standard .aspx file (which will contain HTML code and link to the code behind file), and the second is the actual code behind file (which contains actual application logic and can have any file extension).

[Note] You can use .cs extensions for code behind files that are created in C#. If you’re familiar with VB.NET, you could name your code behind files with a .vb extension. My personal preference, however, is to use the .aspx.cs extension for C# code behind files, and .aspx.vb for VB.NET code behind files. This is a popular code behind naming convention, and it's also the default naming convention for code behind files that are created using Visual Studio.Net. [End Note]

Before showing you a code behind example, let's go over the theory behind it. As you should know by now, when an ASP.NET page is processed by the server, the page automatically inherits from the "Page" base class (for user controls, the page inherits from the "UserControl" base class), which contains all the functionality for rendering HTML code and server controls, as well as many other ASP.NET page processes.

For code behind pages, however, because the page cannot have a .aspx file extension, it doesn't automatically inherit from the "Page" base class (or any base class for that matter). This isn't a big problem; it just means that we have to derive a class from the "Page" base class manually, which I think is a bonus, and not a burden.

[Note] Code behind pages don't have to inherit directly from the "Page" base class. You can create your own base class that inherits from the "Page" base class and use that class as the base class for your code behind page. Doing it this ways allows you to centralize your initialisation code and create a more application-specific base class. [End Note]

Let's look at a code behind example now. Open up notepad and enter the following code. Save the file as sample_codebehind.aspx:

<%@ Page Language="c#" src = "sample_codebehind.aspx.cs" inherits="devArticles.Cdefault" %>

<html>
<head>
<title>Code Behind Example</title>
</head>
<body>
<asp:Label runat= "server" id = "Label1"/>
</body>
</html>


Open up another notepad window and enter the following code. Save the file as sample_codebehind.aspx.cs:

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace devArticles
{
public class Cdefault : Page
{
public Label Label1;
public void Page_Load()
{
Label1.Text = "Hello World... Text from Code Behind";
}
}
}


Run sample_codebehind.aspx in your browser. It should look like this:

Our code behind example in action

Let's take a look at the code in our sample_codebehing.aspx file:

<%@ Page Language="c#" src = "sample_codehehind.aspx.cs" inherits="devArticles.Cdefault" %>

The code above links our ASP.NET page to the C# code behind page that we have created and tells the server which class to inherit from that code behind page. In our case, all of the code contained within the Cdefault class under the devArticles namespace, will be inherited.

We also have a label server control. We modify the value of this labels text property from the code behind file, sample_codebehind.aspx.cs. Let's look at our code behind file now:

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;


The using statements shown above all us to work with server side controls (such as the <asp:label> tag) and manipulate them, amongst other things.

namespace devArticles
{
public class Cdefault : Page
{


Here we define the class and namespace that we have specified as the "inherits" attribute in the first tag of our .aspx page. Notice that we have to manually inherit from the "Page" base class, and if we don't, then our code wouldn't work as we intend it to.

public Label Label1;

This is a crucial part of our code behind page. We declare any controls that we want to use in our main .aspx file here. In our example we have declared a new Label control. For most of the server controls, its class name is the value of what you'd normally write after the first colon in its tag:

<asp:Label>

As you can see, we have created a new label server control. To instantiate a new Label class in our code behind file, we refer to the Label class, which is case sensitive.

private void Page_Load()
{
Label1.Text = "Hello World... Text from Code Behind";
}


The Page_Load() functions is the entry point for our ASP.NET page, and within it we simply set the text property of our label.

You might also like...

Comments

About the author

James Yang Australia

James is a student at Georgia Institute of Technology, majoring in Computer Science. He is an MCSE, MCDBA, MCSA and CCNA.

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“There are 10 types of people in the world, those who can read binary, and those who can't.”