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

Simple way to Design Tabs in ASP.NET

, 11 Jan 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
This article will show how to Create Tabs in ASP.NET page using Buttons, MultiView Control and CSS

Introduction

This is another way to show Tabs in ASP.NET page using Buttons, Multiview Control and CSS.

Background

There are many ways of implementing a Tab in ASP.NET without using AJAX Control.

But many of the solutions use ASP.NET Menu control or a Custom Control to show up the Tab.

Moreover, with the use of ASP.NET menu control, it is not possible to highlight the selected Tab. This is where I came up with a simple approach to show Tabs using Command Buttons, CSS and little code behind code.

How it works ?

This is very simple to Implement.

Basically there will be two CSS styles.

One style will have an initial Background image and another style will have a different Background image to highlight the active Tab. On each button click, the CSS style of the button will be updated from Code Behind.

Below are the simple steps to understand this.

1. Use two images, one for initial styling and another to show the Selected Tab. (I used MS Paint to create these images. Doesn’t require much of skills)

InitialImage.png (Shown Initially)

SelectedButton.png (Shown on Selected Button)

2. Create two different styles, one to show up initially (.Initial and .Initial:hover - to change the style when the mouse pointer is moved over the button) and another to show the Selected Tab (.Clicked).

  <style type="text/css">
  .Initial
  {
    display: block;
    padding: 4px 18px 4px 18px;
    float: left;
    background: url("../Images/InitialImage.png") no-repeat right top;
    color: Black;
    font-weight: bold;
  }
  .Initial:hover
  {
    color: White;
    background: url("../Images/SelectedButton.png") no-repeat right top;
  }
  .Clicked
  {
    float: left;
    display: block;
    background: url("../Images/SelectedButton.png") no-repeat right top;
    padding: 4px 18px 4px 18px;
    color: Black;
    font-weight: bold;
    color: White;
  }
  </style>

3. Create ASP.NET Buttons corresponding to each tab and set the background Image to the initial styling.

a. For each button ensure that the BorderStyle is set to "None" so that it looks like an image and not as a button.

4. Create a Multi View Control and different views corresponding to each tab.

  <body style="font-family: tahoma">
  <form id="form1" runat="server">
    <table width="80%" align="center">
      <tr>
        <td>
          <asp:Button Text="Tab 1" BorderStyle="None" ID="Tab1" CssClass="Initial" runat="server"
              OnClick="Tab1_Click" />
          <asp:Button Text="Tab 2" BorderStyle="None" ID="Tab2" CssClass="Initial" runat="server"
              OnClick="Tab2_Click" />
          <asp:Button Text="Tab 3" BorderStyle="None" ID="Tab3" CssClass="Initial" runat="server"
              OnClick="Tab3_Click" />
          <asp:MultiView ID="MainView" runat="server">
            <asp:View ID="View1" runat="server">
              <table style="width: 100%; border-width: 1px; border-color: #666; border-style: solid">
                <tr>
                  <td>
                    <h3>
                      <span>View 1 </span>
                    </h3>
                  </td>
                </tr>
              </table>
            </asp:View>
            <asp:View ID="View2" runat="server">
              <table style="width: 100%; border-width: 1px; border-color: #666; border-style: solid">
                <tr>
                  <td>
                    <h3>
                      View 2
                    </h3>
                  </td>
                </tr>
              </table>
            </asp:View>
            <asp:View ID="View3" runat="server">
              <table style="width: 100%; border-width: 1px; border-color: #666; border-style: solid">
                <tr>
                  <td>
                    <h3>
                      View 3
                    </h3>
                  </td>
                </tr>
              </table>
            </asp:View>
          </asp:MultiView>
        </td>
      </tr>
    </table>
  </form>
</body>



5. On each button click, set the style of the Clicked button to take a new value.


  public partial class SimpleTabControl : System.Web.UI.Page
  {
    protected void Page_Load(object sender, EventArgs e)
    {
      if (!IsPostBack)
      {
        Tab1.CssClass = "Clicked";
        MainView.ActiveViewIndex = 0;
      }
    }

    protected void Tab1_Click(object sender, EventArgs e)
    {
      Tab1.CssClass = "Clicked";
      Tab2.CssClass = "Initial";
      Tab3.CssClass = "Initial";
      MainView.ActiveViewIndex = 0;
    }

    protected void Tab2_Click(object sender, EventArgs e)
    {
      Tab1.CssClass = "Initial";
      Tab2.CssClass = "Clicked";
      Tab3.CssClass = "Initial";
      MainView.ActiveViewIndex = 1;
    }

    protected void Tab3_Click(object sender, EventArgs e)
    {
      Tab1.CssClass = "Initial";
      Tab2.CssClass = "Initial";
      Tab3.CssClass = "Clicked";
      MainView.ActiveViewIndex = 2;
    }
  }

6. Finally you can get your desired style. (Please use the attached Zip to get the code)

Tabs1.png

Tabs2.png

Conclusion

This is another simple way of Showing Tabs and Highlighting an active tab.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

ASP.NETGuy

United States United States
No Biography provided

Comments and Discussions

 
GeneralVote of execellence PinmemberRitesh bagalkoti11-Nov-14 22:55 
QuestionGood job dude.. Pinmemberumeshkumarbn17-Oct-14 0:48 
SuggestionThis is awesome!!! Pinmembersimpa29-Jul-13 22:06 
SuggestionI Prefer CSS Tabs PinmemberJoe Gakenheimer )26-Jun-13 9:26 
GeneralMy vote of 4 PinmemberJoe Gakenheimer )26-Jun-13 9:24 
GeneralThank you for everything Pinmembermeysambayat1-May-13 20:35 
Generalthankss Pinmemberv_k_s9-Apr-13 21:58 
QuestionMaster pages PinmemberMember2222313-Dec-12 12:24 
AnswerRe: Master pages PinmemberRitesh bagalkoti11-Nov-14 23:00 
GeneralMy vote of 5 PinmemberAmir Amay30-Oct-12 23:58 
QuestionDesign Tabs In asp.net Pinmembersasipriyakalpana24-Sep-12 1:38 
QuestionAdding css in Tabs Dynamically.. PinmemberMember 937108520-Aug-12 23:50 
GeneralMy vote of 4 Pinmemberrahulpriyan22-Jan-12 19:10 
GeneralMy vote of 5 PinmemberVince P.17-Jan-12 9:17 
GeneralMy vote of 5 PinmemberDorababu74311-Jan-12 22:09 
QuestionNice PinmemberPrabu ram11-Jan-12 20:15 
GeneralMy vote of 5 PinmemberAnurag Gandhi11-Jan-12 5:27 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141223.1 | Last Updated 11 Jan 2012
Article Copyright 2012 by ASP.NETGuy
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid