程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> ·Creating Custom Web Controls in C# Stats

·Creating Custom Web Controls in C# Stats

編輯:關於C語言
Rating: 4.75 out of 5 by 4 users  
  Submitted: 04/09/01  


Peter Weng ([email protected])  


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

  
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 controls.


    //start the iteratation at i=1 because we want to disregard the first element
    //of the array after the Split because it will be an empty string. We also
    //want to disregard the last element of the array because that will just be
    //the file name, which will be replaced with the title of the page.
    string[] aPath = Page.Request.Path.Split(new Char[] {'/'});
    int cntPath = aPath.Length;
    string strLinkText = "";
    string strCompleteUrl = "";
    for (int i=1;i < cntPath; i++) {
        Controls.Add(lblSeperator);
                    
        hl = new HyperLink();
        strLinkText = aPath[i].ToString();
        hl.Text = Char.ToUpper(strLinkText[0]) + strLinkText.Substring(1).ToLower();

        strCompleteUrl += "/"+aPath[i].ToString();
        hl.NavigateUrl = strCompleteUrl;
        
        Controls.Add(hl);
    }
  }
}
}



Notice here how we set the aPath string array. We do this by first getting the Path from the Page's HttpRequest Object. Then we'll call C#'s Split method on the Path string. We'll want to split by the '/' character, since our website url is usually in the form http://www.devhood.com/subfolder1/subfolder2/page.ASPx.

Now that we have split the current page's path into a string array, we can loop through this array to build up our Navigation Bar.

At each iteration of the loop, we add the Separator Label to the ControlCollection Object. Next we create a HyperLink Object consisting of the Path name as the Text, and the complete url for that Path as the NavigateUrl. Because this HyperLink Object only links to a Path, or folder in your web site, you'll need to make sure the correct default page loads up automatically when pointing to a folder of your website. For example, Microsoft IIS looks for files such as default.ASPx and default.htm as the page to load when a link points to a path or folder of a website rather than a file or page. The file names and search order can be custom configured in IIS.

When we are finished creating the HyperLink object we'll add it to the ControlCollection Object as well.

Because the Add method on the Controls object appends to the end of the ControlCollection Object, we can be assured that our Navigation Bar will appear in the correct order.

That's it!! We're done. Now all that's left is to compile the code, place the dll file into our web application's bin directory and then we'll be able to use this Web Control on any of our pages by referencing it's namespace and class.

To use this control in an ASP.Net Page, we'll need to add the following directive at the top of each page:


  <%@ Register TagPrefix="MyBar" Namespace="MyNavBar" %>





Then we can render this control on the page with the following line:


<MyBar:NavBar RPad=5 LPad=2 Separator=">" runat=server />




I hope this tutorial has helped you learn how to create custom controls in C#. Now you should be able to go out and create your own Navigation Bar, or other custom control in C#. Remember this Navigation Bar can easily be modified to show page titles instead of file names, or extended by adding propertIEs to customize color, font, font-family, etc. This is left as an exercise for the reader, so go out and create a better more flexible Navigation Bar for your site from the techniques you've learned here. If you have any questions or comments, please post them below.
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved