Tuesday 26 February 2008

Making the AjaxToolKit TabContainer tabs vertical

A chat user asked me the other day of how does one put the tabs in the AjaxToolKit TabContainer vertically and I had no idea. I've decided to do it today and write this blog post, maybe he'll come back and he'd get the answer.

So, the request is simple: take a web site with a TabContainer in it and make it show the tabs vertically. I can only assume that the vertical tabs would go on the left and the content in the right. So I took the Internet Developer Toolbar and analysed the html output of a page with four static tabs. I added a <style> tag with CSS classes and started making changes until it worked. Unfortunately, the same setup would not work on Firefox, so I had to repeat the process using Firebug to analyse the page output. In the end this is the result:
<style>
.ajax__tab_header {
float:left;
}
.ajax__tab_body {
/*float:left;*/
margin-left:220px;
}
.ajax__tab_outer {
display:block !important;
}
.ajax__tab_tab{
/*min-width:200px;*/
width:200px;
height:auto !important;
}
</style>
.

Add this on top of your page or include it in your CSS and the tabs will appear vertically.

Now for a bit of explaining.
  • First of all this does not overwrite the CSS that the TabContainer loads because it is organized under a general ajax__tab_xp class like: .ajax__tab_xp .ajax__tab_header .
  • Then the width of 200px is arbitrary. I used it to keep the vertical tabs at the same width. I tried using min-width first, but it won't display right in Firefox.
  • Another point is about the ajax__tab_body class that I tried to set up first as float left, which would place the body div next to the tabs div, however this breaks if the body tab is wider and the content would appear underneath the tabs div. Thanks to my colleague Romeo I used the margin-left trick. 220px is enough to work in both IE and Firefox. It can be made smaller (closer to 200px)if the default IE body margin would be 0.
  • The !important keyword is placed to overwrite some settings that are already set up in the original TabContainer CSS.
  • Last issue: now the right panel will be truncated if it gets too large. You should control the overflow of that div, although, as far as I am concerned, my job is done


As a kind of disclaimer, I am not a CSS expert. If you know of a better way of doing this, please let me know.

18 comments:

  1. OT:comment

    You seem to have a good grasp of this subject, noting blog from last year - I have an issue with a TabContainer and dynamically adding Tabs.

    The DB returns a list of Types - with maybe six values - and the tabs are supposed to be instantiated to contain small lists of items of that Type in a separate DB Select...

    The problem is I can't tie the Page.LoadTemplate template to a particular DB select statement, it is dependent on the Type.

    Nor do I want to ideally - all I really want to do is assign a populated GridView to each Tab as a prebuilt control - ie.
    thisTab.Controls.Add( gridview );
    thisTabContainer.Add( thisTab );

    Maybe I have missed something. Can you help? Many thanks.

    ReplyDelete
  2. I could help if you explain it better. Can't you use the chat so we can talk in real time?

    From what I understand, you want to create tabs in a tab container dynamically and add stuff to them.

    Doesn't this post help: http://siderite.blogspot.com/2007/07/fixing-tabcontainer-to-work-with.html ?

    Or is the problem that you can't add stuff to them because they are templated controls? I did an application where I was dynamically adding TabPanel objects and I didn't really need an ITemplate. Just:
    TabPanel tb=new TabPanel();
    TabContainer.Tabs.Add(tb);
    tb.Controls.Add([stuff]);

    ReplyDelete
  3. public class TC : TemplateControl, ITemplate
    {
    private GridView _g;
    public GridView Grid
    {
    get { return _g; }
    set { _g = value;
    this.Controls.Add( _g );
    }
    }

    #region ITemplate Members

    public void InstantiateIn( Control container )
    {
    container.Controls.Add( this );
    }

    #endregion
    }


    Is the only way I have found to get over the problem!

    If you add a control to TabPanel using the controls collection, something in AJAX kicks up about a "null handler" property.

    I admit the AJAX documentation is not great!

    ReplyDelete
  4. Hi siderite,

    I applied your style to make the tabs vertical. the tab header are now vertically align, but the tab body is coming below the tab headers.

    Could please suggest or guide to fix this little issue. thanks.

    ReplyDelete
  5. Well, try to use Internet Explorer Developer Toolbar or FireBug to see what is wrong and tweak it until it works. Maybe the width of the screen is too small? What browser are you using?

    Search for me in the chat window.

    ReplyDelete
  6. Your code for vertical tab helps me very much. Thank you.

    ReplyDelete
  7. Does anyone have a link to a working example? Thanks

    ReplyDelete
  8. How can I add a drop down menu to a horizontal tab. I defined a simple ul based listing in HeaderTemplate and it does not work.

    ReplyDelete
  9. Thank you. It helped a lot.

    ReplyDelete
  10. Brilliant thanks. Just what i was looking for.

    ReplyDelete
  11. Wow! Thanks a lot. I've been searching high and low on how to make the tabbed panels veritcally..and your solution is so simple and works perfect!!

    ReplyDelete
  12. THANK YOU! I just added the new TabContainer control from the .Net 4 AjaxToolKit which introduces the vertical tab.

    Problem is that there is a major bug if you use vertical tabs. If you change any default property of the tab or controls in the tabs it throws a JScript error.

    If your interested in more detail with my problem see http://forums.asp.net/p/1760620/4797507.aspx/1?p=True&t=634629383826677143.

    Thanks TyB

    ReplyDelete
  13. Glad I could help. When I wrote this post I don't think they even had vertical tabs as an option.

    ReplyDelete
  14. Thanks from me too. I had the same problem as TyB - TabContainer supports vertical tabs in the current version but they are broken. The script tries to set a 'spanner height' to a negative value, which works in Firefox/Chrome but fails in IE.

    ReplyDelete
  15. Yep - The verticle tab capability in the AJAX tabcontainer has at least a couple of serious bugs which haven't been addressed as of the June 2012 release :(

    They work fine in Chrome and IE9, but break badly in IE8.

    ReplyDelete
  16. thanks its good article

    ReplyDelete
  17. thnx dude ur code helps me....

    ReplyDelete
  18. Great explanation! This solution worked Just About Right!

    ReplyDelete