Click here to Skip to main content
Click here to Skip to main content

Web Tabstrip Control for Frames

By , 13 Jun 2004
 

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 ";<br>
myTab.DisplayPosition = 0;<br>
myTab.URL = "projectHome.aspx?pjID=" + ourID;<br>
TabList2.AddTab(myTab);<br>

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
Web Developer
South Africa South Africa
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#.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralNot Implimentedmemberdeepaks316-Nov-06 3:30 
Hi,
Nice Control, and it saved a lot of tim ...
 
I am just facing this one problem,
Whenever i click on any link of tab control i get this javascript error "Not implimented".
Although it is going to the desired URL. Following is the code i am writing for the Tabs. Please help.

Tab objTab3 = new Tab();
objTab3.DisplayName = " third tab ";
objTab3.DisplayPosition = 2;
objTab3.URL = "abc.aspx";
objTabList.AddTab(objTab3);

 

 
Thanks and regards,
Deepak Surana
QuestionStylesheet??membermo7amad1-Oct-06 4:42 
Please Can you send me how the .css file looks like my email is (mohamad.kafafi@gmail.com)... Confused | :confused:
AnswerRe: Stylesheet??memberDoug Wilson2-Oct-06 7:25 
Well.... thats entirely up to you.
 
The tabstrip control builds up a table, and each cell in that tab has a style applied to it. You set the CssClass property to set the style for all unselected tabs, and the CssClassOver property to the style for the selected tab. These will map back to styles you have created, as does any CssClass property in a .NET Web server control.
 
Doug Wilson

QuestionHow to add panelsmemberjijog5-Aug-06 4:43 
Hi Mr gouge
Your article helped me alot .But iam a beginer in this field.Actually i dont know how add colors inside the tab(between the 4 images) or how to place the each tab in the each panel. Can u help me.
Thank you
jijo
Generalexamples in VB.NETmembersunojiik13-May-06 21:19 
Hi, do u have any sample code for this tabstrip control in VB?
QuestionTabstrip without Frames.memberRiz8126-Jan-06 5:56 
Hi,
 
i am a bit new to .Net, i downloaded this code and it is excellent i really enjoyed working with it. i need to ask you one question that i want to use this component without the Frame.
 
Let put it this way, that i have a div in which i want to place this tab. How can i do it.
 
Please any help would be highly appreciated.
 
And also can you provide the CSS class as well so that i can test it as it looks like in demo.
 
i know its a bit weird. Frown | :(
 
With Best Regards
Mohammad Rizwan Siddiqui
Software Engineer
Infini Logic (Private) Limited
http://www.infinisolve.com
Generalthird party controlsmemberhitesh kala23-Oct-05 20:01 
i have add tabstrip control in asp.net toolbox but it is not working
 
looking just like permanent text. what is the process to add third party controls in tool box.
 
Hitesh Kala
Questionlocation.replace bug ?membermikedepetris14-Apr-05 9:28 
I'm play with this simple thinh but I think there is a bug in the jscript call:
strBuildUp += "window.onload = window.parent.frames[\"" + m_strTargetFrame;
strBuildUp += "\"].location.replace(\"" + GetTabURL(m_intSelTab) + "\");" + "\n";
 
location.replace is a method that changes the frame url, it gives me error when trying to set "windows.onload="
 
where am I wrong? is the uploaded source consistant with the demo dll?
 
I also found I need to put
<FRAME name="top" scrolling="no" src="PolizzaTab.aspx?tabID=0&seltab=0">
 
to make it work, maybe it's I'm used to have script errors detection enabled in IE and you not.
GeneralTabstripmemberonewisehobbit28-Mar-05 5:43 
First off I'm somewhat new to VS(a student at internship). Not afraid to do my own research, however I've hit a brick wall. I'm lost on a couple of issues.
I've created a tabstrip and have put several items(textbox, labels, etc..) on the multipage. However when I create a DDL or radiobutton it likes to place it either on the left margin or upper-left corner, even though I'm changing the Left & Top Properties as follows:
<asp:DropDownList ID="ddlStartMonth" style="Z-INDEX: 113; LEFT 310px; POSITION: absolute; TOP: 55px" Runat="server">
Another issue I'm having is accessing these items(textboxes, labels, etc...). How do I do this (language-wise)? Do i use Javascript, vbscript, ????, and also where I do put it ? Can it be put in the HTML view, or does it have to go in the coding page of VB?
 
PLEASE HELP!
 
Thanks,
Jason
GeneralRe: TabstripmemberDoug Wilson28-Mar-05 23:41 
Hi Jason
 
Ok - first off you have to remember that html by default works in flow layout. So unless you're wrapping each individual item in a div tag and setting the x & y positions on the layer when you render the html, it will inherit the alignment properties of the table cell it's in. The best way to get the alignment right is to render a table with your controls in it to the browser, instead of just the individual items. For your radio buttons, the radio button list control has and alignment property, and you can set it to horizontal or vertical.
 
I'm not quite sure what you mean when you start talking about how to access the controls. Are you adding each control on the tabpage to a placeholder?
If you are, and you're adding server controls, then you should be able to access them by ID, so make sure you assign them ID's as you populate the placeholder.
 
If you're rendering client side html controls (i.e. input tags), then you won't be able to access them directly - especially if you're builing up a literal string or setting the text property of a label for display. You'll need to use javascript for this, and perhaps build up a name value pair string and set a hidden labels text value when you post back, and then iterate through this string list to get your values.
 
Hope this helps.

 
Regards
Doug Wilson
GeneralRe: Tabstripmemberonewisehobbit30-Mar-05 2:12 
Hey Doug,
 
Thanks alot for your response, however I'm still lost on a few things. So if I were to switch to flow layout it would work a little easier? Also you mention a placeholder, should I be using this? The one thing I'm getting stuck on is, when I load my tabstrip (drag & drop onto page) it won't allow me to drag & drop, nor will it allow me to manually draw them. Maybe if I sent you my html it would be easier??? Just curious b/c I'm lost on this stuff.
 
Thanks again.
 
Jason Wise
GeneralRe: Tabstripmemberonewisehobbit30-Mar-05 2:42 
I sent the code to the e-mail I received along with your first reply (mcresin). Hope that was OK.
GeneralGetting Microsoft JScript runtime errormemberrajshekarreddy1-Mar-05 22:08 
HI,
I downloaded the demo project, When I run the application,
 
I got a error in the page Tab.aspx at line "Request.QueryString["seltab"].Length == 0" . So I commented the code and executed the project,
Then project got executed I was able to see the tabs.
But When I clicked one of the tab("Consultants"). I got the following Run time Error.
 
Microsoft JScript runtime error: 'window.parent.frames.bottom.location' is null or not an objectpjID=\");\n</script>"
 
How to solve it?
 
I debuged the code, In ChangeTab() function of Tab.cs class(Which is inside the Component Assembly).The strBuildUp variable has following value.
 
"<script language=JavaScript>window.onload = window.parent.frames[\"bottom\"].location.replace(\"prjConsultants.aspx?pjID=\");\n</script>"
 
is this the expected value is correct?
 
Please help me out where i am going wrong or am i missing anything.
 
Thanks in advance,
 
Regards,
Rajshekar Reddy.N

 
sdfgsdfgsdfg
GeneralParse Error in .aspx and ascxmembermj61625-Jan-05 16:30 
Hi Doug,
Thanks for the help with the tablist. I do have a question Concerning a parse error I keep receiving:
============================================================================
Line 1: <%@ Register TagPrefix="cc1" Namespace="doug.Components" Assembly="Components" %>
Line 2: <%@ Page language="c#" Codebehind="Mytabs1.aspx.cs" AutoEventWireup="false" Inherits="ASPNET.StarterKit.Commerce.Mytabs1" %>
Line 3: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
Line 4: <HTML>

 
Source File: E:\Program Files\PayPal\ASP.NET Commerce Starter Kit for PayPal\PayPalCommerce\Mytabs1.aspx Line: 2

============================================================================
I am a fairly new C#.net programmer so I am wondering if you could give me some assistance with this. Also I am receiving build errors for the Request.QueryString object, could this be causing my problems with the parse in the aspx page.
 

Thanks,
Mike
GeneralRe: Parse Error in .aspx and ascxmemberDoug Wilson25-Jan-05 19:29 
Hi Mike
 
You didn't include the base error message - so the following is just a guess.
 
You may be referencing you code behind class incorrectly. If your code behind file looks something like this:
 
namespace MyProject
{
public class MyTabs1
{
//code here
}
}
 
then you'll reference it in you page directive with something like:
 
Inherits="MyProject.MyTabs1"
 
What I'm trying to illustrate here is that it's reference format is Namespace.ClassName.
 
For you Request.Querystring - again, no error message so heres a guess. A common problem is referencing a variable that doesn't exist. You get around this be implementing defensive programming techniques, so you must first expect it to be missing, and so check to see if it's there before you do anything with it.
 
For example:
 
if (Request.Querystring["bob"] != null)
{
string strBob = Request.Querystring["bob"].ToString();
}
 
Hope this helps.

 
Regards
Doug Wilson
Generalgive me code to use tabStrip controlmembermanish ranjan sinha25-Oct-04 2:30 
i want some code in asp.net that uses property of tabStrip control especially when one moves from one tab to other without saving data then a client side validation is performed ie. user gets a proper messagebox
 
manish sinha
GeneralRe: give me code to use tabStrip controlmemberDoug Wilson26-Oct-04 5:25 
Don't be lazy - use the source provided as a base and write your own code to do all the validation etc. If you need javascript help I'd suggest starting at www.w3schools.com.
 
If you just got someone to give you code all the time - then you shouldn't be earning a salary.
 
Come on - apply your self Smile | :)
 
Doug Wilson
GeneralNot ImplementedmemberLarri4-Aug-04 22:01 
i have changed links to my files in the Tabs.aspx.cs, after running it fails in the some javascript row " window.onload = window.parent.frames["bottom"].location.replace("blank.htm");" and gives error "Not implemented".
 
How i can change the code for running it well?
thanks for the answer
GeneralRe: Not ImplementedmemberJehosephat8-Nov-04 6:42 
I'm also running into this problem. I thought it had something to do with a readonly property of parent frames but I can't figure out a way around this. It doesn't affect functionality, but it does pop up a script error.
GeneralRe: Not ImplementedmemberDoug Wilson16-Nov-04 22:39 
Hi Guys
 
Please could you send me the javascript error (mcresin@hotmail.com), as well as snippets of code where you're using the control (i.e. where you add your tabs etc).
 
I'll have a look and see if i can work out where the problem lies.
 
Cheers

 
Doug Wilson
GeneralRe: Not ImplementedmemberJehosephat17-Nov-04 6:25 
I think the problem ended up being something to do with window.onload not liking the return value of window.parent....replace()
I replaced:
 
window.onload = window.parent.frames["bottom"].location.replace("blank.htm");"
 
with:
 
window.onload = function(){var oFrame = window.parent.frames["tabBody"];oFrame.location.replace("ActionsMain.aspx");
 
And that seemed to work. One problem with this is that if you decide to implement any onload=xxx() in your html, it will overwrite the above. Haven't figured out a good way to have both yet.
GeneralRe: Not Implementedmembersh171731-Jan-05 6:04 
IS this the best solution for this??? Do I need to edit the source code with this change in the ChangeTab event and recompile, or is there an easier fix???
 
Thanks
GeneralHELP!memberBeginnerProgrammer0072-Aug-04 19:36 
I have beginner questions about the above project which is what I was looking for exactly.
 
1) Where do I find the CSS file "..\CSS\mtt.css"
 
2) Building the project gives me an error as "e:\inetpub\wwwroot\TabsList\Tabs.aspx.cs(19): The type or namespace name 'doug' could not be found (are you missing a using directive or an assembly reference?)"
 
Help please. Thanks.
 
ME
GeneralRe: HELP!memberDoug Wilson2-Aug-04 19:51 
Hi there
 
1) a .css file is a style sheet file. You should build your own style sheet and apply your styles to the control to customize it's appearance.
 
2) You haven't referenced the dll in your project. If you are using Visual Studio, right click References for your web project and browse to the dll included in the download file. If you are using another IDE, place the dll in the bin directory of your web project.
 
Regards
 
Doug Wilson
GeneralRe: HELP!memberBeginnerProgrammer0073-Aug-04 0:31 
Thanks a lot Doug, it works now and it is very much appreciated.
 
Me

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130617.1 | Last Updated 14 Jun 2004
Article Copyright 2004 by Doug Wilson
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid