当前位置:编程学习 > C#/ASP.NET >>

Creating Custom Web Controls in C# Stats(转)

答案:Creating Custom Web Controls in C#    Stats  
  Rating: 4.75 out of 5 by 4 users  
  Submitted: 04/09/01  


Peter Weng (lateboy@mit.edu)  


--------------------------------------------------------------------------------

  
By now you may or may not have heard of ASP.NET's Web Controls. In this tutorial I plan to discuss the advantages of using Web Controls, as well as how to create and deploy them in C#.

What are Web Controls?

Web Controls are widgets that can be reused across multiple webpages. Controls may render HTML to the client, or other formats such as WML or XML. If you're familiar with ASP.NET, you'll know that ASP.NET provides some controls for you right out of the box. These include DataGrids, Calendars, AdRotators, etc.

Why use User Controls?

The biggest advantage of Web Controls is code reuse. Because they can be reused across multiple pages in different ways, Web Controls can save you the hassle of writing similar HTML on multiple pages by generating the appropriate HTML for you.

But why use C#? Can't we just use .ascx files?
.ascx files are another way of creating reusable controls. However, it doesn't always provide a clean way of separating your code from content. What this means is that content developers can write content, graphic artists and UI experts can layout your site, and you can develop the logic behind everything without overlapping each other. In addition, when deploying your web site or giving your web site to someone else, by using controls written in C# and pre-compiled, you won't have to give them any of your source code to launch your site, all you need to give them are the dll's which contain your compiled source code for your control.

What are we going to build today?
Today we'll be taking apart one of the components of DevHood. If you look at the top of most of our pages, you'll notice a Yahoo! style navigation bar that looks something like this:



Home >   Tutorials >   General ASP.NET >   Creating Custom Web Controls



This navigation bar is actually generated dynamically for each page by a Custom Web Control. Let's start by looking at the first part of the code.


namespace MyNavBar {
    using System;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Collections;
    using System.Drawing;



We begin by giving our Web Control a namespace, so that we can reference it in other pages. We'll also import all the namespaces we'll need for our Web Control. On .aspx pages, the following namespaces are imported by default, but for Web Controls in .cs files, you'll need to import them yourself if you'll need them:
System
System.Web
System.Web.UI
System.Web.UI.HtmlControls
System.Web.UI.WebControls
System.Collections
System.IO


public class NavBar : WebControl {
    private string strSeperator = ">";
    private int iRPad = 3;
    private int iLPad = 1;

    public string Seperator {
        get {return strSeperator;}
        set {strSeperator = value;}
    }

    public int LPad {
        get {return iLPad;}
        set {iLPad = value;}
    }

    public int RPad {
        get {return iRPad;}
        set {iRPad = value;}
    }



This section of code defines the class for the Web Control, which is NavBar, and specifies it is a public class which can be instantiated by any page. We also speciify that NavBar should derive from the WwebControl class. Remember that since we imported then System.Web.UI.WebControls class we didn't need to use the full-qualified namespace reference to the WebControls class. i.e, we could just use WebControl instead of public class NavBar : System.Web.UI.WebControls.WebControl.

Next we define class variables that we'll use in building our Navigation Bar. We'll want the developer to be able to specify properties of the control such as what will be used to separate the elements, and the spacing between the seperator. You'll see what've defined class variables to hold these properties, mainly strSeperator, iLPad, iRPad. However, in order to set these properties in a page, we define the getter/setter methods for them as above.

Now we can finally get to the heart of the code. The WebControl class contains an overrideable method CreateChildControls() which is called whenever the WebControl is initialized. This is where all of our Control creation will be done.


protected override void CreateChildControls() {
            
    //build up the Right Padding string.
    string strRPad = String.Empty;
    for (int i = 0; i < iRPad; i++) {
        strRPad += " ";
    }

    //build up the Left Padding string.
    string strLPad = String.Empty;
    for (int i = 0; i < iLPad; i++) {
        strLPad += " ";
    }
        
    Label lblSeparator;
    lblSeparator = new Label();
    lblSeparator.Text = strLPad + strSeparator + strRPad;




Again we'll want to start out by performing some initialization steps. First we'll build up a string containing the amount of white spaces as specified by the RPad and LPad properties. Remember, we defined the getter/setter properties for these class variables, so they can be changed by the user. The default values for them are 3 and 1, since we initialized them to these values when we defined the variables above. We also create a Label control lblSeparator which, as the name suggests, will be the separator between elements of the navigation bar. This label consists of the left padding, concatenated with the user specified Separator string (this is the string ">" by default), along with the right padding.


    //iterate through the arraylist of hyperlinks and place the seperators and padding
    //string in between the hyperlinks.
    HyperLink hl;
        
    hl = new HyperLink();
    hl.Text = "Home";
    hl.NavigateUrl = "/";
    Controls.Add(hl);



Here we begin by declaring then initializing a HyperLink object. The first element of the navigation bar will always be pointing to "Home", and it's NavigateUrl will always be "/" (the root directory of your web site). Notice the line Controls.Add(hl). This adds the created "Home" HyperLink to the NavBar WebControl's ControlCollection Object. The ControlCollection Object provides a container for the NavBar to keep a list of its child contro

上一个:Regular Expression Library(1)
下一个:C#编程入门三部曲

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,