Click here to Skip to main content
11,706,979 members (46,648 online)
Click here to Skip to main content

Simple way to Design Tabs in ASP.NET

, 11 Jan 2012 CPOL 285.4K 9.4K 40
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

You may also be interested in...

Comments and Discussions

 
GeneralVote of execellence Pin
Ritesh bagalkoti11-Nov-14 21:55
memberRitesh bagalkoti11-Nov-14 21:55 
QuestionGood job dude.. Pin
umeshkumarbn16-Oct-14 23:48
memberumeshkumarbn16-Oct-14 23:48 
SuggestionThis is awesome!!! Pin
simpa29-Jul-13 21:06
membersimpa29-Jul-13 21:06 
SuggestionI Prefer CSS Tabs Pin
Joe Gakenheimer )26-Jun-13 8:26
memberJoe Gakenheimer )26-Jun-13 8:26 
GeneralRe: I Prefer CSS Tabs Pin
veasnamuch21-Apr-15 18:23
memberveasnamuch21-Apr-15 18:23 
GeneralRe: I Prefer CSS Tabs Pin
jgakenhe21-Apr-15 18:37
memberjgakenhe21-Apr-15 18:37 
GeneralMy vote of 4 Pin
Joe Gakenheimer )26-Jun-13 8:24
memberJoe Gakenheimer )26-Jun-13 8:24 
GeneralThank you for everything Pin
meysambayat1-May-13 19:35
membermeysambayat1-May-13 19:35 
Generalthankss Pin
v_k_s9-Apr-13 20:58
memberv_k_s9-Apr-13 20:58 
QuestionMaster pages Pin
Member2222313-Dec-12 11:24
memberMember2222313-Dec-12 11:24 
AnswerRe: Master pages Pin
Ritesh bagalkoti11-Nov-14 22:00
memberRitesh bagalkoti11-Nov-14 22:00 
GeneralMy vote of 5 Pin
Amir Amay30-Oct-12 22:58
memberAmir Amay30-Oct-12 22:58 
QuestionDesign Tabs In asp.net Pin
sasipriyakalpana24-Sep-12 0:38
membersasipriyakalpana24-Sep-12 0:38 
QuestionAdding css in Tabs Dynamically.. Pin
Member 937108520-Aug-12 22:50
memberMember 937108520-Aug-12 22:50 
GeneralMy vote of 4 Pin
rahulpriyan22-Jan-12 18:10
memberrahulpriyan22-Jan-12 18:10 
GeneralMy vote of 5 Pin
Vince P.17-Jan-12 8:17
memberVince P.17-Jan-12 8:17 
GeneralMy vote of 5 Pin
Dorababu74311-Jan-12 21:09
memberDorababu74311-Jan-12 21:09 
QuestionNice Pin
Prabu ram11-Jan-12 19:15
memberPrabu ram11-Jan-12 19:15 
GeneralMy vote of 5 Pin
Anurag Gandhi11-Jan-12 4:27
memberAnurag Gandhi11-Jan-12 4: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
Web02 | 2.8.150819.1 | Last Updated 11 Jan 2012
Article Copyright 2012 by ASP.NETGuy
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid