Click here to Skip to main content
Licence 
First Posted 22 Jun 2006
Views 91,332
Bookmarked 85 times

Dynamic ASP.NET Form Tabs

By | 20 Sep 2006 | Article
A custom control for generating form tabs for web applications.

Introduction

Ever needed to display form tabs for a web application dynamically on separate pages? This custom control does just that. The content is driven dynamically, and it is easy to implement.

I was tired of generating countless lines of HTML on separate pages, just to have someone say I want it to look like this. So, I developed a custom .NET control to handle the generation of form tabs.

The colors are driven by styles, and will be included with this example.

The code-behind implementation is really simple. Because someone suggested it, I've added a ClientID property that sets the 'ID' of that table cell, and I have also included a property that is set in the Properties window to allow the developer to decide whether to include the CheckDirty logic.

'control declaration.
Protected WithEvents FormTab1 As FormTabs.FormTab
Dim tab As FormTab = FormTab1
'Parameters (Tab Text, Client ID, Tab URL, blnSelected)
tab.AddTab("Tab 1", "tab1", "", True) 
tab.AddTab("Tab 2", "tab2", "Tab2.aspx?ID=" & txtID.Text, False)
tab.AddTab("Tab 3", "tab3", "Tab3.aspx?ID=" & txtID.Text, False)

Which would generate something like this:

Your .aspx page will look like this:

<table cellSpacing="0" cellPadding="0" width="100%" border="0">
  <tr>
    <td class="tabrow">
      <cc1:formtab id="FormTab1" runat="server">
      </cc1:formtab>
    </td>    
  </tr>
</table>

The first parameter gives the tab the name. That is what will show up on the tab. The second parameter is a URL that the user will navigate to when clicked. And as you can see, it takes variables into the URL. The third parameter, and maybe not needed if you change the logic to look at an empty URL string, is a boolean value that determines whether that tab is the active tab. So, in a normal situation, there should only be one active tab, and the active tab would change based on the page the user was on.

You need to include these styles in your application:

.bottomline {border-bottom: #57A2DC 1px solid;}
.tabrow {background-color:#f9fbe0;}
.tab {background-color: #E7F2F8; color: #666666; 
      line-height:18px; text-align: center; 
      border: #57A2DC 1px solid;}
.tab_hover {background-color: #E3F2BB; color: #000000; 
            line-height:18px; text-align: center; 
            border: #57A2DC 1px solid; 
            cursor: hand; cursor:pointer;}
.tab_selected {background-color: #ffffff; color: #000000; 
               text-decoration: none; text-align: center; 
               height: 18px; border-left: #57A2DC 1px solid; 
               border-bottom: #ffffff 1px solid; 
               border-right: #57A2DC 1px solid; 
               border-top: #57A2DC 1px solid;}

These styles should be self-explanatory.

To use this control as is, you'll need to do the following:

  • Add the DLL to your VS toolbox
  • Drag the control to a Web page
  • Add the styles to your Web page
  • Include the CheckDirty.js file in your Web page

I generally include the "CheckDirty"* functionality to check if the user has changed anything on the form before navigating to another page. Because of that, the CheckDirty.js file will need to be included with your application. Or, remove the reference to it in the control. There are numerous articles on implementing this functionality, so I won't go into it here. Just know, it's built in to this control.

** I've updated the code to allow the developer to set whether they want to include this functionality. Get the CheckDirty file here:

As you can see, this control gives you the ability to add form tabs to Web forms with great ease. You can use this control to control access to certain pages for security reasons. Just build in the logic to check for authorization. Maybe something like this:

If strUser = "Admin" Then
    tab.AddTab("Admin Console", "ClientID1", "admin.aspx", True|False)
Else
    tab.AddTab("User Console", "ClientID2", "user.aspx", True|False)
End If

Improved functionality (hopefully)

I've added a little more functionality to the form tabs. I've taken dropdown menus from Dynamic Drive and incorporated that into this control. So, now you can have "child" tabs that fly out from the "parent" tab. There's are a couple more properties to set to accomplish this result:

Your code will look something like this:

'tabs.AddTab(Tab Text, URL string, ClientID, IsSelected, HasChildren)
tabs.AddTab("Categories", "", "categories", False, True)
tabs.AddChildTab("categories", "Books", "books.aspx")
tabs.AddChildTab("categories", "Music", "music.aspx")

tabs.AddTab("Help", "Menu2.aspx", "help", True, True)
tabs.AddChildTab("help", "Contact Us", "contact.aspx")
tabs.AddChildTab("help", "Privacy Policy", "privacy.aspx")

I've also updated the source files (Download updated source code - 27.5 Kb) so that it includes a solution with the custom control project as well as a Web project for getting it up and running easier. The solution also contains an external JS file that controls the dropdown menus, so you need to include that file in your project should you need the dropdown functionality. Should you not need the dropdown functionality in your project, just set the HasChildren property to False.

I played with several menu systems, but found that the one from Dynamic Drive works the best (and easiest). If anyone has any suggestions on further improving this control, let me know.

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

About the Author

CraigCampbell

Web Developer

United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralRegarding Event Handler in our control PinmemberMember 31310506:02 3 Oct '08  
GeneralSelected Item not working Pinmemberclayson4:26 12 Dec '07  
Hi there I'm having a bit of an issue getting the selected tab to work. I'm not sure what property to use in the FormTabs control because there is no SelectedIndex.
If anyone has got it working could you please help me out.
GeneralRe: Selected Item not working PinmemberCraigCampbell5:04 15 Aug '08  
GeneralTab Click event Pinmemberbyka8:25 13 Nov '07  
QuestionHow do I detect when I am leaving a tab? Pinmemberdgoolsby10:31 26 Sep '07  
AnswerRe: How do I detect when I am leaving a tab? PinmemberCraigCampbell5:08 15 Aug '08  
QuestionTabs Font Pinmemberkeyser-soze2:47 13 Sep '07  
QuestionHow to automatically select the tab? Pinmembermischiefmantra8:29 8 Jan '07  
AnswerRe: How to automatically select the tab? PinmemberCraigCampbell8:52 8 Jan '07  
QuestioncheckDirty not working Pinmemberkkmothe5:24 31 Oct '06  
AnswerRe: checkDirty not working PinmemberCraigCampbell7:19 31 Oct '06  
QuestionRe: checkDirty not working Pinmemberkkmothe5:51 9 Nov '06  
QuestionUsing Form Tabs on same page Pinmemberhim_patel11:27 13 Oct '06  
AnswerRe: Using Form Tabs on same page PinmemberCraigCampbell3:57 16 Oct '06  
GeneralSemantic Markup Pinmemberthe_ox21:28 25 Sep '06  
GeneralRe: Semantic Markup PinmemberCraigCampbell2:13 26 Sep '06  
GeneralGood One PinmemberMichael_Dhasu0:10 20 Sep '06  
GeneralRe: Good One Pinmembervik2019:14 20 Sep '06  
GeneralRe: Good One PinmemberCraigCampbell7:25 31 Oct '06  
GeneralSelect tab manualy Pinmembermorteza5722:44 24 Jul '06  
GeneralRe: Select tab manualy PinmemberCraigCampbell2:01 25 Jul '06  
GeneralRe: Select tab manualy Pinmembermorteza572:47 25 Jul '06  
GeneralSample Page PinmemberDewey16:26 15 Jul '06  
QuestionCheckDirty.js?? PinmemberPalanisamy Veerasingam22:51 11 Jul '06  
Questionwhere is CheckDirty.js? PinmemberGökmen BULUT3:40 11 Jul '06  

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.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120529.1 | Last Updated 20 Sep 2006
Article Copyright 2006 by CraigCampbell
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid