Click here to Skip to main content
6,595,444 members and growing! (15,878 online)
Email Password   helpLost your password?
Web Development » ASP.NET Controls » General     Intermediate

Web Tabstrip Control for Frames

By Doug Wilson

Building a custom ASP.NET Tabstrip Control
C#, Windows, .NET, ASP.NET, Visual Studio, Dev
Posted:28 Apr 2004
Updated:13 Jun 2004
Views:181,207
Bookmarked:54 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
21 votes for this article.
Popularity: 4.50 Rating: 3.41 out of 5
6 votes, 28.6%
1
3 votes, 14.3%
2
3 votes, 14.3%
3
5 votes, 23.8%
4
4 votes, 19.0%
5

Introduction

There are plenty of tabstrip controls available for download that use panels to decide which content to display. But what if your existing web application's / web site's content is stored on separate pages, or you have to use frames? This article will take you through the basics of creating a custom tabstrip control that can be loaded in one frame, and change content in another frame.

Using the code

Using the code is quite simple. The library contains a Tab control, as well as a TabList Control. You add Tabs to the TabList. To demonstrate, very quickly,

myTab.DisplayName = " Project Home ";
myTab.DisplayPosition = 0;
myTab.URL = "projectHome.aspx?pjID=" + ourID;
TabList2.AddTab(myTab);

The TabList Control has a number of properties that you can set, namely:

Settings Properties

  1. UseFrames (default is true - provided so that I can incorporate panel tabs later)
  2. TargetFrame (the name of the frame to load the content pages in)
  3. SelectedTab (the index of the selected tab)
  4. TabsAcross (the number of horizontal tabs before creating a new line)

Appearance Properties

  1. LeftImage (the image to appear on the left hand side of the tab)
  2. RightImage (the image to appear on the right hand side of the tab)
  3. TopImage (the image to appear on the top of the tab)
  4. BottomImage (the image to appear below the tab)
  5. LeftImageOver (the image to appear on the left hand side of the tab when the tab is selected)
  6. RightImageOver (the image to appear on the right hand side of the tab when the tab is selected)
  7. TopImageOver (the image to appear on the top of the tab when the tab is selected)
  8. BottomImageOver (the image to appear below the tab when the tab is selected)
  9. CssClass (the style sheet element for the unselected tabs)
  10. CssClassOver (the style sheet element for the selected tab)

How it all works

The Tablist Control has a private member named m_items which is an arraylist of Tab. This is where the collection of tabs are stored. To order the tabs (by the tab display position) I had to have a way to compare the tabs to one another. For that I created a TabCompare class, inheriting from IComparer.

This allowed me to compare one tabs display position to another, and return a value representing whether the first tab should appear before or after the second tab. By inheriting from the IComparer interface, I could now also make use of the arraylist method Sort, which, as it now had a way to compare one tab to another, could automatically order my tabs for me, simply by calling m_items.Sort(new TabCompare()), which I wrapped in a private method called order.

I also wanted my control to be able to have fancy images around it (you know, so it looks like a tab), so my control renders 3 html rows per tab line (the top row for the top image, the middle row for the side images and tab, and the bottom row for the bottom images). I wrote a method that returns a string for each of the rows.

The ChangeTab method is the event handler for the linkbuttons (which is what i used to display the tab link and handle click events), and simply changes the selected tab index, and uses JavaScript to change the target page.

State Management

Because my TabList control inherits from Control, I can override the LoadViewState and SaveViewState methods to maintain the state of the control. The only value I needed to maintain was the selected tab index, m_intSelTab.

  //Saves the View State

  protected override object SaveViewState() 
  {
   object[] allStates = new object[2];
   allStates[0] = base.SaveViewState();
   allStates[1] = m_intSelTab;
   return allStates;
  }
  //Loads View State Info 'Load Saved State Values

  protected override void LoadViewState(object savedState) 
  {
   if (savedState != null)
   {
    object[] myState = (object[])savedState;
    if (myState[0] != null) 
    {
     base.LoadViewState(myState[0]);
    }
    if (myState[1] != null) 
    {
     m_intSelTab = Convert.ToInt32(myState[1]);
    }
   }
  }
  

In my CreateChildControls method (which renders the control to the screen) I simply set the start position (index of first tab to display - i.e. 0), and work out how many tabs to display in the first row. I also set a counter to keep track of which tabs I've displayed. I then order the tabs by calling the order method I created before.

I then loop through all my tabs and write them to the screen, using my counters to determine when to draw a new row, and when to finish off the table. I used linkbuttons as the hyperlinks, so that I could control their events server side.

  protected override void CreateChildControls() 
  {
   int intColRows = (m_items.Count / m_intTabsAcross) + 1;
   int intPositionRendered = 0;
   int intStartPosition = 0;

   order();

   this.Controls.Clear();

   this.Controls.Add(new LiteralControl(
"<table cellpadding=\"2\" cellspacing=\"1\" border=\"0\" ID="Table1">"));

   for (int i=1; i <= intColRows; i++) 
   {
    if ((m_strTopImage.Length > 0) || (m_strSelTopImage.Length > 0))
    {
     this.Controls.Add(new LiteralControl(TopRow(intStartPosition, 
         ref intPositionRendered, m_intSelTab)));
    }
    MiddleRow(intStartPosition, ref intPositionRendered, m_intSelTab);
    if ((m_strBottomImage.Length > 0) || (m_strSelBottomImage.Length > 0)) 
    {
     this.Controls.Add(new LiteralControl(BottomRow(intStartPosition, 
        ref intPositionRendered, m_intSelTab)));
    }
    intStartPosition = intPositionRendered;
   }

   this.Controls.Add(new LiteralControl("</table>"));

  }
  

And that's it. I hope you find both the control (I included the dll for use), and this article useful.

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

About the Author

Doug Wilson


Member
Doug is an Applications Integrator for an online gaming company. He has been programming for 9 years, and has been working with the .NET framework since the beginning of 2003, in both VB.NET & C#.
Occupation: Web Developer
Location: South Africa South Africa

Other popular ASP.NET Controls articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 40 (Total in Forum: 40) (Refresh)FirstPrevNext
GeneralNot Implimented Pinmemberdeepaks34:30 16 Nov '06  
QuestionStylesheet?? Pinmembermo7amad5:42 1 Oct '06  
AnswerRe: Stylesheet?? PinmemberDoug Wilson8:25 2 Oct '06  
GeneralHow to add panels Pinmemberjijog5:43 5 Aug '06  
Generalexamples in VB.NET Pinmembersunojiik22:19 13 May '06  
QuestionTabstrip without Frames. PinmemberRiz816:56 26 Jan '06  
Generalthird party controls Pinmemberhitesh kala21:01 23 Oct '05  
Generallocation.replace bug ? Pinmembermikedepetris10:28 14 Apr '05  
GeneralTabstrip Pinmemberonewisehobbit6:43 28 Mar '05  
GeneralRe: Tabstrip PinmemberDoug Wilson0:41 29 Mar '05  
GeneralRe: Tabstrip Pinmemberonewisehobbit3:12 30 Mar '05  
GeneralRe: Tabstrip Pinmemberonewisehobbit3:42 30 Mar '05  
GeneralGetting Microsoft JScript runtime error Pinmemberrajshekarreddy23:08 1 Mar '05  
GeneralParse Error in .aspx and ascx Pinmembermj61617:30 25 Jan '05  
GeneralRe: Parse Error in .aspx and ascx PinmemberDoug Wilson20:29 25 Jan '05  
Generalgive me code to use tabStrip control Pinmembermanish ranjan sinha3:30 25 Oct '04  
GeneralRe: give me code to use tabStrip control PinmemberDoug Wilson6:25 26 Oct '04  
GeneralNot Implemented PinmemberLarri23:01 4 Aug '04  
GeneralRe: Not Implemented PinmemberJehosephat7:42 8 Nov '04  
GeneralRe: Not Implemented PinmemberDoug Wilson23:39 16 Nov '04  
GeneralRe: Not Implemented PinmemberJehosephat7:25 17 Nov '04  
GeneralRe: Not Implemented Pinmembersh17177:04 31 Jan '05  
GeneralHELP! PinmemberBeginnerProgrammer00720:36 2 Aug '04  
GeneralRe: HELP! PinmemberDoug Wilson20:51 2 Aug '04  
GeneralRe: HELP! PinmemberBeginnerProgrammer0071:31 3 Aug '04  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 13 Jun 2004
Editor: Nishant Sivakumar
Copyright 2004 by Doug Wilson
Everything else Copyright © CodeProject, 1999-2009
Web21 | Advertise on the Code Project