Home arrow ASP.NET arrow Page 4 - ASP.NET Controls Explained: Part 1/2
ASP.NET

ASP.NET Controls Explained: Part 1/2


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.

Author Info:
By: James Yang
Rating: 5 stars5 stars5 stars5 stars5 stars / 56
February 03, 2002
TABLE OF CONTENTS:
  1. · ASP.NET Controls Explained: Part 1/2
  2. · User Controls
  3. · HTML Controls and Server Controls
  4. · Code Behind
  5. · Conclusion

print this article
SEARCH DEVARTICLES

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:

<%@ 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.
blog comments powered by Disqus
ASP.NET ARTICLES

- How Caching Means More Ca-ching, Part 2
- How Caching Means More Ca-ching, Part 1
- Reading a Delimited File Using ASP.Net and V...
- What is .Net and Where is ASP.NET?
- An Object Driven Interface with .Net
- Create Your Own Guestbook In ASP.NET
- HTTP File Download Without User Interaction ...
- Dynamically Using Methods in ASP.NET
- Changing the Page Size Interactively in a Da...
- XML Serialization in ASP.NET
- Using Objects in ASP.NET: Part 1/2
- IE Web Controls in VB.NET
- Class Frameworks in VB .NET
- Cryptographic Objects in C#: Part 1
- Sample Chapter: Pure ASP.Net

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials