Click here to Skip to main content
15,878,748 members
Articles / Web Development / ASP.NET
Article

ASP.NET TabBar Server Control

Rate me:
Please Sign up or sign in to vote.
4.59/5 (17 votes)
22 Aug 20061 min read 132.6K   1.8K   102   39
A customizable ASP.NET 2.0 TabBar server control with designer support, written in VB.NET.

tabbar render example

Introduction

A lot of my clients request tabbed navigation in their web applications. Most of the applications I work on are business type applications, and a favorite navigation scheme is to have a tab bar to represent high-level categories, with sub categories represented with a "sub" tab bar. So off I went to develop a control that would provide a visual representation of a tab bar that could be configured to be page aware or agnostic, integrates with the built-in ASP.NET roles API, has designer support, supports viewstate serialization, and could be easily manipulated programmatically.

Adding the tab bar to a page

First, add a reference to GeminiSoftworks.Web.Navigation.dll.

Next, add the following to your web.config file as a child of <system.web>.

XML
<pages>
    <controls>
        <add tagPrefix="TabBar" 
             namespace="GeminiSoftworks.Web.Navigation" 
             assembly="GeminiSoftworks.Web.Navigation"/>
        <add tagPrefix="Tab" 
             namespace="GeminiSoftworks.Web.Navigation" 
             assembly="GeminiSoftworks.Web.Navigation"/>
    </controls>
</pages>

Last, add the control to a page. This can be done by adding the control to your studio toolbox and then dragging the control on to the page, or by adding it directly to the HTML like so:

HTML
<TabBar:TabBar ID="TabBar1" runat="server" Width="100%">
    <TabBar:Tab Link="~/default.aspx" Name="Home" PreserveFormOnTransfer="False" 
                Text="Home" ToolTip="Home" UseServerTransferMethod="False" 
                Visible="True" />
    <TabBar:Tab Link="" Name="Resources" PreserveFormOnTransfer="False" 
                Text="Resources" ToolTip="Resources" 
                UseServerTransferMethod="False" Visible="True" />
    <TabBar:Tab Link="" Name="Downloads" PreserveFormOnTransfer="False" 
                Text="Downloads" ToolTip="Downloads" 
                UseServerTransferMethod="False" Visible="True" />
</TabBar:TabBar>

Now, open the page in a browser and your page should render like the example image above.

Working with the tab bar in code

C#
protected void TabBar1_TabClick(object sender, 
          System.ComponentModel.CancelEventArgs ce)
{
    //basic tabbar operations...

    //example: get the tab that was clicked
    GeminiSoftworks.Web.Navigation.Tab t = 
           (GeminiSoftworks.Web.Navigation.Tab)sender;

    /* 
     * The following examples will modify the tabbar programmatically 
     * I will cancel the click event at the end of this function so that
     * the changes made will render
     * 
     * Typically, I will define constants for all the tab objects I need
     * and programmatically render the tabbar based on the circumstances.
     * The only case I declaritively set up tabs
     * is when they are basically static.
     * 
     * ** Remember **
     * If you are using the standard ASP.NET roles
     * configuration, secured tabs will be 
     * automatically suppressed for users that
     * don't have the appropriate authority to
     * use them. You can also create a derived
     * class and override the IsTabValidForRole
     * to implement custom security.
     * 
     * Roles = * (all users), ? (authenticated users),
     *            and any roles you define
     * 
     * ** Remember **
     * Tab authentication is based on the least secure role
     * (e.g. a tab with roles ("Admin","*") will render for all roles. 
     * 
     */

    //example: create a new tab
    GeminiSoftworks.Web.Navigation.Tab nt = 
    new GeminiSoftworks.Web.Navigation.Tab("NewTab", 
             "New Tab", "~/newpage.aspx", "New Tab");

    //example: set some pages that the tab should render 'active' for...
    nt.ActivePagesList = new string[] { "~/newsubpage1.aspx", 
                                        "~/newsubpage2.aspx" };

    //example: assign a role to view the tab
    //note: not assigning any roles means all users can view the tab, so
    //this example is the same as not setting the AllowRoles property
    nt.AllowRoles = new string[] { "*" };

    //example: add the new tab to the tabbar
    this.TabBar1.Tabs.Add(nt);

    //example: remove a tab by calling GetTab 
    //which accepts a tab name and returns a tab reference
    this.TabBar1.Tabs.Remove(this.TabBar1.Tabs.GetTab("Downloads"));

    /* cancel the click event to see the changes we made above */

    //example: cancel the event
    ce.Cancel = true;
}

Things you can learn from the source

  • Designer support
  • Embedding web resources
  • Viewstate serialization
  • Using HTML rendering instead of composite control rendering
  • Supporting events

What still needs doing

At some point, I will add a few more things to the tab bar. Specifically, it needs ADA section 508 support, and the ability to use custom images instead of text for the tabs. One day, there will be a few hours free to do this, but instead of taking care of these enhancements at this time, I decided to submit this article in hopes others will find it useful. So please enjoy...

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 3 Pin
Dhritirao's17-Oct-13 0:18
Dhritirao's17-Oct-13 0:18 
QuestionHow to remove the extra tab bar? Pin
kennett51511-Oct-11 7:57
kennett51511-Oct-11 7:57 
GeneralMy vote of 5 Pin
Syed J Hashmi15-Jun-11 2:43
Syed J Hashmi15-Jun-11 2:43 
GeneralHelp friend! Pin
njuniorba6-Feb-09 2:35
njuniorba6-Feb-09 2:35 
Generaladding a reference Pin
bigshahman2-Dec-08 4:45
bigshahman2-Dec-08 4:45 
QuestionActive/Inactive images Pin
JoeLeTaxiUK17-Nov-08 0:05
JoeLeTaxiUK17-Nov-08 0:05 
QuestionHow Use enable use images of ActiveTab Pin
player.14-Oct-08 2:42
player.14-Oct-08 2:42 
at Test.aspx

<TabBar:TabBar ID="TabBar1" runat="server" Width="100%" OnInit="TabBar1_Init" OnTabClick="TabBar1_TabClick">
</TabBar:TabBar>

at Test.aspx.cs

using GeminiSoftworks.Web.Navigation;

protected void TabBar1_Init(object sender, EventArgs e)
{
TabBar thisTabBar = sender as TabBar;

thisTabBar.Tabs.Add(new Tab("-1", "All"));
thisTabBar.Tabs.Add(new Tab("0", "X0")); //0
thisTabBar.Tabs.Add(new Tab("1", "X1")); //1
thisTabBar.Tabs.Add(new Tab("2", "X2")); //2
}

----
but, it can not use files of images, about atab_border.gif, atab_center.gif, atab_left.gif, atab_right.gif

Why? How todo enable use images of ActiveTab?

Taiwan's pauper.

AnswerRe: How Use enable use images of ActiveTab Pin
player.14-Oct-08 17:13
player.14-Oct-08 17:13 
QuestionRecognize query string? Pin
Gary In SD8-Jul-08 7:43
Gary In SD8-Jul-08 7:43 
AnswerRe: Recognize query string? Pin
corebytes8-Jul-08 13:38
corebytes8-Jul-08 13:38 
GeneralRe: Recognize query string? Pin
Gary In SD8-Jul-08 14:14
Gary In SD8-Jul-08 14:14 
QuestionWebConfig Changes Pin
stixoffire2-May-08 10:21
stixoffire2-May-08 10:21 
AnswerRe: WebConfig Changes Pin
corebytes8-Jul-08 13:52
corebytes8-Jul-08 13:52 
AnswerRe: WebConfig Changes Pin
stixoffire8-Jul-08 18:20
stixoffire8-Jul-08 18:20 
GeneralRe: WebConfig Changes Pin
ShepherdBook19-Jan-10 6:39
ShepherdBook19-Jan-10 6:39 
QuestionHow can i select active tab dynamically Pin
kodalimani21-Jun-07 17:35
kodalimani21-Jun-07 17:35 
AnswerRe: How can i select active tab dynamically Pin
pzlk23-Jul-07 23:16
pzlk23-Jul-07 23:16 
GeneralAwesome Pin
Zodraz3-Apr-07 4:41
Zodraz3-Apr-07 4:41 
QuestionHow use this tabBar? Pin
lamik28-Feb-07 20:45
lamik28-Feb-07 20:45 
QuestionActivePageList and L&#304;nk not working in Tab Bar Pin
TanjuOzgur2-Oct-06 23:57
TanjuOzgur2-Oct-06 23:57 
AnswerRe: ActivePageList and L&#304;nk not working in Tab Bar Pin
TanjuOzgur3-Oct-06 23:56
TanjuOzgur3-Oct-06 23:56 
QuestionPlace the desired page in a container Pin
Nizam Farid Ahmed20-Sep-06 2:19
Nizam Farid Ahmed20-Sep-06 2:19 
AnswerRe: Place the desired page in a container Pin
corebytes20-Sep-06 16:02
corebytes20-Sep-06 16:02 
Generalautomatic casding of tab control Pin
breakpoint11-Sep-06 3:10
breakpoint11-Sep-06 3:10 
AnswerRe: automatic casding of tab control Pin
corebytes11-Sep-06 7:28
corebytes11-Sep-06 7:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.