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:
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.
ASP.NET Controls Explained: Part 1/2
Code Behind
You might also like...
About the author
James Yang
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.
ASP.NET books
-
20 Recipes for Programming MVC 3: Faster, Smarter Web Development
There's no need to reinvent the wheel every time you run into a problem with ASP.NET's Model-View-Controller (MVC) framework. This concise cookbook provides recipes to help you solve tasks many web developers encounter every day. Each recipe includes...
ASP.NET forum discussion
-
edmonton female escort services near me
by canadapleasure (0 replies)
-
USB Drive Activity Logger!
by coque0912 (7 replies)
-
Bagaimana memenangkan $ 1,54 miliar dalam Mega Jutaan
by gametogelan (0 replies)
-
input integer from text file and output text file
by shmilon (0 replies)
-
cSharp stuck at exercise
by xander_Michiels (0 replies)
ASP.NET podcasts
-
.NET Rocks: Jeff Fritz Knows One ASP.NET
Published 7 years ago, running time 0h54m
Carl and Richard talk to Jeff Fritz about how ASP.NET has evolved over the years. Jeff talks about the continued dominance of web forms in relation to MVC and how the different libraries can be used together. In fact, there's ONE ASP.NET, and everything: web forms, MVC, Web API, SignalR, all work.
ASP.NET jobs
-
.NET Web Developer at FinTech Startup (with traction)
FundApps in London, United Kingdom
Negotiable -
.NET Developer
sti-professionalservices in Detroit, United States
FOR ALL POSITIONS COMPENSATION IS NEGOTIABLE -
Web Developer at Growing Startup
FundApps in London, United Kingdom
-
Senior Software Engineers
LexisNexis in Cary, United States
Comments