Only 3 simple steps to create jQuery UI tabs content navigation in your ASP.NET Website

jQuery is definitely the most popular JavaScript library in the web.

Together with jQuery, the jQuery UI library becomes very popular too. The reasons why jQuery UI library is popular are that its still lightweight, and with almost no coding you get astonishing features.

One of the features that you may often see in the websites is the tabs-based content navigation. In one of my recent projects, I’ve had a requirement to implement tabbing system and since the project is based on ASP.NET WebForms and jQuery, I considered going with the jQuery UI tabs.

YOU NEED TO TAKE ONLY THREE STEPS TO MAKE IT WORKING!

  1. Reference necessary scripts and css files
  2. Create HTML/ASPX content that will follow the rules for a tabs-based content navigation feature
  3. Add one line jQuery Script Code

1. Reference necessary scripts and css files

All the scripts we need are in the Microsoft CDN (Content Delivery Network)

<link type="text/css" href="http://ajax.microsoft.com/ajax/jquery.ui/1.8.9/themes/blitzer/jquery-ui.css" rel="Stylesheet" />
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.4.4.js"></script>
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.9/jquery-ui.min.js"></script>

The first link is the theme I’ve chosen from the available themes in the Microsoft Ajax CDN jQuery UI Themes.

The scripts are the jQuery 1.4.4 library and the jQuery UI 1.8.9 minified.

 

2. Create HTML/ASPX content that will follow the rules for a tabs-based content navigation feature

The main HTML structure is

3. Add one line jQuery Script Code

I think this is the most difficult part

<script type="text/javascript">
    $(function () {
        $("#myTabs").tabs();
    });
</script>

Now you see why :).


So, here is a complete example:

<!DOCTYPE html>
<html lang="en">
<head id="Head1" runat="server">
    <title>jQuery UI Tabs - Default functionality</title>
    <link type="text/css" href="http://ajax.microsoft.com/ajax/jquery.ui/1.8.9/themes/blitzer/jquery-ui.css"
        rel="Stylesheet" />
    <script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.4.4.js"></script>
    <script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.9/jquery-ui.min.js"></script>
    <script type="text/javascript">
        $(function () {
            $("#myTabs").tabs();
        });
    </script>
</head>
<body>
    <div class="content">
        <div id="myTabs" style="width:500px;">
            <ul>
                <li><a href="#tab-1">Title 1</a></li>
                <li><a href="#tab-2">Title 2</a></li>
                <li><a href="#tab-3">Title 3</a></li>
            </ul>
            <div id="tab-1">
                <p>
                    Lorem Ipsum is simply dummy text of the printing and typesetting industry.<br />
                    Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when
                    an unknown printer took a galley of type and scrambled it to make a type specimen
                    book.
                    <br />
                    It has survived not only five centuries, but also the leap into electronic typesetting,
                    remaining essentially unchanged.
                </p>
            </div>
            <div id="tab-2">
                <p>
                    It is a long established fact that a reader will be distracted by the readable content
                    of a page when looking at its layout.
                    <br />
                    The point of using Lorem Ipsum is that it has a more-or-less normal distribution
                    of letters, as opposed to using 'Content here, content here', making it look like
                    readable English.
                    <ul>
                        <li>List Item 1</li>
                        <li>List Item 2</li>
                    </ul>
                </p>
            </div>
            <div id="tab-3">
                <p>
                    There are many variations of passages of Lorem Ipsum available, but the majority
                    have suffered alteration in some form,<br />
                    by injected humour, or randomised words which don't look even slightly believable.
                </p>
            </div>
        </div>
    </div>
</body>
</html>

 

And the result is:

If you want to make the tab navigation a bit more fancy, add fx option in the following way:

$("#myTabs").tabs({ fx: {opacity:"toggle"} });

If you use multiple ASP.NET Panels <asp:Panel /> on a page, you can easily make tabs out of these panels

For example, your panel will be in the following way:

<asp:Panel ID="tab11" runat="server">
    My Panel here
</asp:Panel>

In client side, it generates:

<div id="tab11">
    My Panel here
</div>

The rest you know :).

Hope this was helpful.

Please do let me know your feedback.

15 Comments

  • Great tutorial, thanks!
    One question... let's say you are in tab "Title 2" and there is a "Submit" button that results in a postback. How would make sure that the page loads in the same tab (Title 2) and not the default "Title 1"?

  • @Kal, thank you for your comment and your question.

    I've got the same question in my blog post in Macedonian language (link: http://mkdot.net/blogs/hajan/archive/2011/01/27/16894.aspx) and the answer is:

    You can, for example add HiddenField where you will save the selected tab index. Then, modify the code where you call tabs in the following way:

    $("#myTabs").tabs({
    select: function (event, ui) {
    $("#selectedTab").val(ui.index); },
    selected:
    });

    I assume the HiddenField has ID "selectedTab" you can save the selected tab using select: event.

    In the last part:
    selected:

    you will retrieve the last selected tab after postback. You can do the state management yourself by using any of the methods for passing data tru pages or keeping data on postback.

    I'm thinking to write new blog post showing ways to do this correctly... If you think it's worth, please let me know and I will post new blog on this topic soon.

    Hope this helps.
    Hajan

  • Thanks thanks......very much. So helpfull

  • Nothing happened with this 3 simple steps. I did exactly as you have mentioned, however, the end result was ugly looking list

  • Anshuman, you probably did something wrong... Did you add the script and style the same way I have added in the blog? Do you have same HTML structure? Please go to http://forums.asp.net/1223.aspx and post your question with link to this blog. Post your code there and you will get help by me or other contributors/answerers in the forum.

    Thanks

  • Hi..
    Thanks for sharing this information and resources it's really help full for me with the help of this we can improve our designs and development I really inspire with this information thanks

  • Hi,

    First of all thanks for great example, made my life easy. But I would like to change the color scheme and font size. If you can help, that will be great.

    Thanks

  • Okay, may be I wasn't clear enough in my ealier post. I want to change the color behind the tab (Red) and color of tab text (Red).

    I figured out the font size:


    Thanks

  • @Javed, as you can see, I am using stylesheet loaded directly from CDN:


    You can go to Microsoft CDN and chose one of the stylesheet templates (http://www.asp.net/ajaxlibrary/CDNjQueryUI189.ashx) or if none of these fits for you, then you can either go to http://jqueryui.com/themeroller/ and build style yourself, then you can download it locally... or the last option (which I do ofte :) )... is to download the stylesheet from themeroller or the same one from CDN and make changes on it... its properly commented, so you can find the styles 'tabs' plugin and play with it...

    Please do let me know your progres...

  • I try to just copy and paste all code, but when i open the index.php for example i have some problem, first all the contetnt of jquery is not organized very well, i know the problem, i use one external css and one css with link, the link is the same whith you code, how to solve this problem, do i need to change the external css? or do i need to do somthing for jquery?

  • Thanks for the helpful example. can you please explain this line
    "selected: " from below code###
    or if you can give the working example that would be great help.
    You can, for example add HiddenField where you will save the selected tab index. Then, modify the code where you call tabs in the following way:
    ###Code

    ---------------- ------------------------
    $("#myTabs").tabs({

    select: function (event, ui) {

    $("#selectedTab").val(ui.index); },

    selected:

    });

    I assume the HiddenField has ID "selectedTab" you can save the selected tab using select: event.

    In the last part:

    selected:

    you will retrieve the last selected tab after postback. You can do the state management yourself by using any of the methods for passing data tru pages or keeping data on postback.


    ----------------------
    Thanks
    Richa

  • I am facing an issue when I moved my javascript into an external js file.
    the tab selection stopped working now again I am going to the first tab on postback.
    getting hiddenfield (selectedTab) value as undefined.
    I know it is related to the order of how js files are being called.

    any help is much appriciated.

    Thanks
    -Richa

  • when i try to run this program i am getting following error message

    'Microsoft JScript runtime error: Object doesn't support property or method 'tabs''

    I've already provided reference but still giving an error.

    and one more thing i need nested tab control:tabs with in a tab..
    please advise me.

    thanks

  • where exactly should i put the theme folder after saving it .. and what should i name the folder in order for the website to accept it???

  • This is all and good, but I'd like to see an example of loading content into the tabs through AJAX. Any decent sized application will have its content broken up across many pages.

Comments have been disabled for this content.