Click here to Skip to main content
15,886,806 members
Articles / Web Development / ASP.NET
Article

A Simple ASP.NET Tab Control Using the MultiView control

Rate me:
Please Sign up or sign in to vote.
4.61/5 (88 votes)
22 Feb 20063 min read 1.4M   33.5K   197   102
A simple tab control using the ASP.NET MultiView and Menu controls.

Introduction

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:

HTML
<asp:Menu
        ID="Menu1"
        Width="168px"
        runat="server"
        Orientation="Horizontal"
        StaticEnableDefaultPopOutImage="False"
        OnMenuItemClick="Menu1_MenuItemClick">
    <Items>
        <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>
    </Items>
</asp:Menu>

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.

HTML
<asp:MultiView 
    ID="MultiView1"
    runat="server"
    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
                    CHANGE SELECTED IMAGE URL AS NECESSARY
                </td>
            </tr>
        </table>
     </asp:View>
    <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
                    CHANGE SELECTED IMAGE URL AS NECESSARY
                </td>
            </tr>
        </table>
    </asp:View>
    <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
                  CHANGE SELECTED IMAGE URL AS NECESSARY
                </td>
            </tr>
        </table>
    </asp:View>
</asp:MultiView>

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

VB
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"
        Else
            Menu1.Items(i).ImageUrl = "unselectedtab.gif"
        End If
    Next
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.

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


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionHow to play an MS PowerPoint show on ASP.Net Pin
khuzwayom15-Jun-07 3:33
khuzwayom15-Jun-07 3:33 
QuestionHow to play an MS PowerPoint show on the ASP.Net page Pin
khuzwayom15-Jun-07 3:33
khuzwayom15-Jun-07 3:33 
QuestionHow to play an MS PowerPoint show on the ASP.Net page Pin
khuzwayom15-Jun-07 3:32
khuzwayom15-Jun-07 3:32 
GeneralSimilar functionality using c# Pin
YRKS6-Jun-07 5:24
YRKS6-Jun-07 5:24 
GeneralRe: Similar functionality using c# Pin
Srinivasa_kumar9-Jul-07 9:35
Srinivasa_kumar9-Jul-07 9:35 
Questiondid not work in the master page Pin
cashier30-May-07 16:54
cashier30-May-07 16:54 
AnswerRe: did not work in the master page Pin
Brian Rush31-May-07 2:56
Brian Rush31-May-07 2:56 
GeneralRe: did not work in the master page Pin
Indrajeetkumpavat20-Apr-09 23:21
Indrajeetkumpavat20-Apr-09 23:21 
AnswerRe: did not work in the master page Pin
Jats_4ru26-Jun-07 2:09
Jats_4ru26-Jun-07 2:09 
QuestionONLY Image in menu item Pin
mehulmistry14-May-07 1:09
mehulmistry14-May-07 1:09 
AnswerRe: ONLY Image in menu item Pin
raghu619-May-07 0:28
raghu619-May-07 0:28 
GeneralVeryNice Pin
raji_7926@yahoo.co.in3-Apr-07 2:29
raji_7926@yahoo.co.in3-Apr-07 2:29 
QuestionHow to avoid postback? Pin
judyIsk13-Mar-07 0:35
judyIsk13-Mar-07 0:35 
QuestionRe: How to avoid postback? Pin
vineelach30-Apr-09 5:19
vineelach30-Apr-09 5:19 
QuestionPossibility to stay at the same view after postback? Pin
pejuta wichasa12-Jan-07 1:45
pejuta wichasa12-Jan-07 1:45 
AnswerRe: Possibility to stay at the same view after postback? Pin
gonzaling9-Mar-07 1:50
gonzaling9-Mar-07 1:50 
Questionhow do i Pin
suju3120-Nov-06 20:46
suju3120-Nov-06 20:46 
AnswerRe: how do i Pin
Brian Rush21-Nov-06 13:00
Brian Rush21-Nov-06 13:00 
QuestionHow to add a text Pin
just420-Nov-06 19:50
just420-Nov-06 19:50 
AnswerRe: How to add a text Pin
Brian Rush21-Nov-06 12:56
Brian Rush21-Nov-06 12:56 
QuestionDynamic Tabs Pin
sanjaybhagia28-Oct-06 5:12
sanjaybhagia28-Oct-06 5:12 
QuestionHow to we make it such that each tab bring us to a new page? Pin
whoam116-Oct-06 15:29
whoam116-Oct-06 15:29 
AnswerRe: How to we make it such that each tab bring us to a new page? Pin
Jats_4ru26-Jun-07 2:13
Jats_4ru26-Jun-07 2:13 
GeneralRe: How to we make it such that each tab bring us to a new page? Pin
Srinivasa_kumar9-Jul-07 9:37
Srinivasa_kumar9-Jul-07 9:37 
GeneralPositive Feedback Pin
MichaelCullina21-Sep-06 5:53
MichaelCullina21-Sep-06 5:53 

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.