Skip to main content
Email Password   helpLost your password?

Screenshot - TabStrip.jpg

Introduction

The Microsoft ASP.NET Ajax Toolkit offers excellent tab control that organizes several views within a single page when each view is presented one at a time. It is more difficult to use this control to manage tasks that are split between different pages. The TabStrip control presented in this article could be used for navigation between multiple pages.

Background

The TabStrip control is adapted from the ASP.NET Ajax Toolkit tab control. It holds set of Tab objects that represent header text only. The TabStrip control does not include templates to define page content. The Tab elements are populated either from an XML file or defined in the page markup. A page URL can be associated with each tab, with a property indicating whether it is disabled. Disabled tabs cannot be selected. A tab can contain one layer of nested hyper links that are also defined in the XML file.

The control fires the ActiveItemChanged event on a server and the ClientActiveItemChanged event on a client. The server side event carries information about the tab being clicked, such as the ID and URL. The client side event also holds the ID of the currently selected tab and allows cancellation of a tab change.

Using the code

The control has to be explicitly registered using a directive:

<%@ Register Assembly="TabStrip" 
    Namespace="AjaxControlToolkit" TagPrefix="act" %>

The control usage can be illustrated by the markup code:

<act:TabStrip runat="server" ID="TabStrip1" DataFile="~/TabMap.xml"
        OnActiveItemChanged="OnActiveItemChanged1"
        OnClientActiveItemChanged="ClientActiveItemChanged" />

The tabs can be initialized as shown in the code below:

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        TabStrip1.DataBind();
    }
}

The data XML file has the following format:

<ArrayOfTabMap>
  <TabMap Text="Tab #1" >
    <ArrayOfTabMap>
        <TabMap Url="Default.aspx" Text="Sub #1" />
        <TabMap Url="SubPage2.aspx" Text="Sub #2" />
        <TabMap Url="SubPage3.aspx" Text="Sub #3" Disabled="true" />
    </ArrayOfTabMap>
  </TabMap>
  <TabMap Url="Page2.aspx" Text="Tab #2" Disabled="true" />
  <TabMap Url="Page3.aspx" Text="Tab #3" />
  <TabMap Text="Tab #4" DefaultActiveSubItemIndex="1">
    <ArrayOfTabMap>
      <TabMap Url="SubPage4.aspx" Text="Sub #4" />
      <TabMap Url="SubPage5.aspx" Text="Sub #5" />
    </ArrayOfTabMap>
  </TabMap>
</ArrayOfTabMap>

In this sample, the first layer of TabMap elements defines the top level navigation tabs. The optional nested TabMap entries define navigation link elements within the current tab. If the TabMap entry has child elements then it should not have an associated page URL. If the TabMap element has the Disabled attribute set, then subsequent tab or link control won't be clickable and will be rendered in faded colors. A TabMap element could have the attribute DefaultActiveSubItemIndex defined. In this case, upon selection of the tab the nested hyperlink with the corresponding index is selected.

The server side OnActiveTabChanged event could be fired when the selected tab changes. It contains the ID of the currently selected tab and target page URL. The event has the following signature:

protected void OnActiveItemChanged1(object sender, 
    ActiveItemChangedEventArgs e)
{
    Response.Redirect(e.Url);
}

However, the handling of this event is not necessary for the navigation. The control allows no-code data binding. The event OnClientActiveItemChanged is generated when the tab changes on a client. The event carries the tab ID and allows cancellation of the event propagation to the server:

<script type="text/javascript">
function ClientActiveItemChanged(sender, e) 
{
    var result = 
        confirm("You are about to switch to the tab with ID " + 
        e.get_id() + ".\nAre you sure?");
    if (!result)
       e.set_cancel(true);
}
</script>

To change the control default appearance, the custom CSS file could be referenced by the page and the new style could be associated with the control when the page loads:

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
       TabStrip1.CssClass = "my_tab";
       TabStrip1.DataBind();
    }
}

History

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralProblem with sub tabs and css Pin
maxitaxi
9:40 9 Sep '09  
GeneralNow Tab Creation is very easy with Asp.net Pin
dilipsss
22:40 21 May '09  
GeneralRe: Now Tab Creation is very easy with Asp.net Pin
vgapp
1:30 22 May '09  
GeneralProblem to run the project Pin
dilipsss
3:31 15 May '09  
GeneralRe: Problem to run the project Pin
vgapp
3:57 15 May '09  
GeneralRe: Problem to run the project Pin
dilipsss
3:28 18 May '09  
GeneralRe: Problem to run the project Pin
vgapp
4:12 19 May '09  
GeneralRe: Problem to run the project Pin
Hariprasd_G
1:45 16 Jul '09  
GeneralBuilding the tabstrip w/o xml Pin
Member 2813991
15:03 15 Oct '08  
Generallink from other websites Pin
cristtiah
11:34 14 Oct '08  
GeneralCreate Tab Strip from database Pin
himanshu2561
22:58 5 Sep '08  
GeneralRe: Create Tab Strip from database Pin
vgapp
8:28 9 Sep '08  
GeneralCssClass help Pin
sun_rang
13:47 7 Jul '08  
GeneralRe: CssClass help Pin
vgapp
5:41 8 Jul '08  
GeneralSetting ID in xml file Pin
sebsebserb
17:24 14 Jun '08  
GeneralUsing Database Pin
heffen
13:14 2 May '08  
GeneralAJAX Version Pin
JohnnyHax
15:44 25 Apr '08  
GeneralRe: AJAX Version Pin
vgapp
5:13 28 Apr '08  
GeneralRe: AJAX Version Pin
JohnnyHax
20:41 28 Apr '08  
QuestionAdd new tab Pin
Parthasarathy Mandayam
8:21 5 Nov '07  
AnswerRe: Add new tab Pin
vgapp
17:20 5 Nov '07  
GeneralDisplay an active tab without having to use the PostBack button. Pin
DaveBrighton
20:00 26 Sep '07  
GeneralRe: Display an active tab without having to use the PostBack button. Pin
DaveBrighton
5:17 27 Sep '07  
GeneralRe: Display an active tab without having to use the PostBack button. Pin
vgapp
8:39 27 Sep '07  
GeneralRe: Display an active tab without having to use the PostBack button. Pin
DaveBrighton
16:09 27 Sep '07  


Last Updated 6 Jul 2007 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2009