ASP.NET
  Home arrow ASP.NET arrow Page 4 - ASP.NET Controls Explained: Part 1/2
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ASP.NET

ASP.NET Controls Explained: Part 1/2
By: James Yang
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 6
    2002-02-03

    Table of Contents:
  • ASP.NET Controls Explained: Part 1/2
  • User Controls
  • HTML Controls and Server Controls
  • Code Behind
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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.

    More ASP.NET Articles
    More By James Yang


     

    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







    © 2003-2010 by Developer Shed. All rights reserved. DS Cluster 11 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek