Click here to Skip to main content
14,601,227 members

A Simple ASP.NET Tab Control Using the MultiView control

Rate this:
4.61 (88 votes)
Please Sign up or sign in to vote.
4.61 (88 votes)
22 Feb 2006
A simple tab control using the ASP.NET MultiView and Menu controls.


Recently, I had been writing a small web scrapping application using ASP.NET 2.0. During which time I was trying to investigate some of the new controls available in ASP.NET 2.0. One of the 2.0 controls that I found particularly interesting was the MultiView control. The new MultiView control works in tandem with an embedded View control. Each View control acts as a content container, and behaves very much like a Panel or Canvas area. At run time, the MultiView control manages the View controls to ensure only one View is visible at a time. We can use the ActiveViewIndex property of the MultiView control to specify which View should be visible at any particular time. Sounds kind of like the Tab control right? What I wanted to do was implement a quick tab control without having to purchase a commercial component. Disclaimer, the commercial components are much more robust and probably worth the case but if you don't have the cash or don't need the "cadillac" then the following code provides a very simple solution.

About the control

When I approached writing my simple tab control, I had this article in the back of my mind, CSS and Round Corners: Build Accessible Menu Tabs: a nice article on how to make a tab control using CSS tricks and images.

What I wanted to do was combine the ideas in this CSS article with the ASP.NET Menu and MultiView controls to create a relatively nice looking tab control. This is what I came up with:

First, I dropped a Menu control onto my page. The Menu control was going to render my "tabs" and also be the controller of the MultiView control. I set the menu to be orientated horizontally. Likewise, I gave each MenuItem an ImageURL that pointed to my tab image. Note, you will have to implement your own tab image as you see fit. Below is my Menu control specification:







        <asp:MenuItem ImageUrl="~/selectedtab.GIF" 

                      Text=" " Value="0"></asp:MenuItem>
        <asp:MenuItem ImageUrl="~/unselectedtab.GIF" 

                      Text=" " Value="1"></asp:MenuItem>
        <asp:MenuItem ImageUrl="~/unselectedtab.GIF" 

                      Text=" " Value="2"></asp:MenuItem>

Next, I put my MultiView control onto my page. Note, each View control contains a table with a <td> tag that will hold all the contents for the active tab. Think of the <td> element as your placeholder for the tab contents. I put this <td> element in a CSS class called "TabArea". This class puts a border around the contents. Refer to the stylesheet in the sample download.




    ActiveViewIndex="0"  >
   <asp:View ID="Tab1" runat="server"  >
        <table width="600" height="400" cellpadding=0 cellspacing=0>
            <tr valign="top">
                <td class="TabArea" style="width: 600px">
                    <br />
                    <br />
                    TAB VIEW 1
                    INSERT YOUR CONENT IN HERE
    <asp:View ID="Tab2" runat="server">
        <table width="600px" height="400px" cellpadding=0 cellspacing=0>
            <tr valign="top">
                <td class="TabArea" style="width: 600px">
                <br />
                <br />
                    TAB VIEW 2
                    INSERT YOUR CONENT IN HERE
    <asp:View ID="Tab3" runat="server">
        <table width="600px" height="400px" cellpadding=0 cellspacing=0>
            <tr valign="top">
                <td class="TabArea" style="width: 600px">
                <br />
                <br />
                  TAB VIEW 3
                  INSERT YOUR CONENT IN HERE

Last, I wired the MenuItemClick event of the Menu control to the method below:

Protected Sub Menu1_MenuItemClick(ByVal sender As Object, _
          ByVal e As MenuEventArgs) Handles Menu1.MenuItemClick
    MultiView1.ActiveViewIndex = Int32.Parse(e.Item.Value)
    Dim i As Integer
    'Make the selected menu item reflect the correct imageurl
    For i = 0 To Menu1.Items.Count - 1
        If i = e.Item.Value Then
            Menu1.Items(i).ImageUrl = "selectedtab.gif"
            Menu1.Items(i).ImageUrl = "unselectedtab.gif"
        End If
End Sub

In this event, you can see that I set the pertinent view to be visible based upon which tab (really a MenuItem) was clicked. Likewise, I set the correct ImageUrl for the selected MenuItem. This is basically swapping in and out images for the tabs. This should be the image of what you want a selected tab to look like. In my example, I have two images, a selected tab and an unselected tab. This is what my example looks when "tab 2" is selected:

Image 1

This control basically rides on some basic CSS styling and the ASP.NET Menu and MultiView controls. Pretty simple, right?

Hope you can find some use for it.


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

Brian Rush
United States United States
No Biography provided

Comments and Discussions

QuestionGap between image and content Pin
krrish60221-Feb-17 5:24
Memberkrrish60221-Feb-17 5:24 
GeneralMy vote of 3 Pin
Bala Ballzz17-Jul-14 19:28
MemberBala Ballzz17-Jul-14 19:28 
QuestionNo Text? Pin
panania12-Nov-13 8:28
Memberpanania12-Nov-13 8:28 
GeneralMy vote of 3 Pin
Kanzz_25412-Jun-13 23:48
MemberKanzz_25412-Jun-13 23:48 
QuestionMouseover Tabs Menu Pin
MS Babu10-May-13 20:16
MemberMS Babu10-May-13 20:16 
QuestionMy vote of 4 Pin
Alireza_136214-Mar-13 23:05
MemberAlireza_136214-Mar-13 23:05 
QuestionDoesn't work with Chrome Pin
Gary Huck6-Aug-12 9:02
MemberGary Huck6-Aug-12 9:02 
AnswerRe: Doesn't work with Chrome Pin
robindegen16-Aug-12 3:00
Memberrobindegen16-Aug-12 3:00 code Pin
jaipal090811-Jun-12 3:17
Memberjaipal090811-Jun-12 3:17 
AnswerRe: code Pin
Gary Huck6-Aug-12 8:50
MemberGary Huck6-Aug-12 8:50 
QuestionTabs are not switching!! No event on clicking Menu Items!!Please help...Urgent!! Pin
Chinthu Krishnan20-Mar-12 10:05
MemberChinthu Krishnan20-Mar-12 10:05 
QuestionThanks Pin
Arsemaz111-Jan-12 23:20
MemberArsemaz111-Jan-12 23:20 
GeneralMy vote of 1 Pin
naresh@hits27-Sep-11 21:05
Membernaresh@hits27-Sep-11 21:05 
QuestionChanging to Other Views is Not Working Pin
RichardM320-Jul-11 11:11
MemberRichardM320-Jul-11 11:11 
Generalmy menu items are not showing in horizontal Pin
reddygmail14-Jun-11 23:42
Memberreddygmail14-Jun-11 23:42 
GeneralRe: my menu items are not showing in horizontal Pin
Member 25095830-Jun-11 11:41
MemberMember 25095830-Jun-11 11:41 
GeneralMy vote of 4 Pin 23:44 23:44 
GeneralNice One! Pin
feeza01257-Apr-11 21:00
Memberfeeza01257-Apr-11 21:00 
GeneralMy vote of 5 Pin
SamuelZhang883-Mar-11 18:44
MemberSamuelZhang883-Mar-11 18:44 
GeneralObsolete but good- use Ajax now Pin
Frank Jammes6-Dec-10 1:40
MemberFrank Jammes6-Dec-10 1:40 
GeneralMy vote of 5 Pin
shekhar bhosle18-Aug-10 0:35
Membershekhar bhosle18-Aug-10 0:35 
GeneralSign up form just as biultin createuser wizard Pin
vicky martyn15-Aug-10 3:57
Membervicky martyn15-Aug-10 3:57 
GeneralThank you!!!! Pin
ssneed25-May-10 19:35
Memberssneed25-May-10 19:35 
GeneralThank You Pin
Lucas Santos Sanches4-Nov-09 13:46
MemberLucas Santos Sanches4-Nov-09 13:46 
GeneralGood control with simple programming Pin
vineetas328-Aug-09 3:56
Membervineetas328-Aug-09 3:56 

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.

Posted 22 Feb 2006


195 bookmarked