In ASP.NET, there are several features that we can use to make our code reusable and independent of other code, including user controls, server controls, and the code behind method. In this article James explains what each of these features are used for, and also shows you how to use each one by providing a number of simple yet detailed ASP.NET examples.
ASP.NET Controls Explained: Part 1/2 - Code Behind (Page 4 of 5 )
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).
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.
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.
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.
Let's look at a code behind example now. Open up notepad and enter the following code. Save the file as sample_codebehind.aspx:
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:
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.
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:
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.