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

ASP.NET Ajax Toolkit TabStrip Control

Rate me:
Please Sign up or sign in to vote.
3.14/5 (15 votes)
6 Jul 20072 min read 171K   2.6K   34   35
A tab strip control based on the ASP.NET Ajax Control Toolkit

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:

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

The control usage can be illustrated by the markup code:

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

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

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

The data XML file has the following format:

XML
<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:

C#
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:

JavaScript
<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:

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

History

  • 06/01/2007: First version

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
Canada Canada
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionnot working Pin
VLRRAJ14-Mar-14 0:00
VLRRAJ14-Mar-14 0:00 
GeneralMy vote of 1 Pin
Sonu Garg23-Aug-11 16:24
Sonu Garg23-Aug-11 16:24 
GeneralNot working with more then 2 level xml hierarchy Pin
DimaMuradov31-Mar-10 9:17
DimaMuradov31-Mar-10 9:17 
GeneralRe: Not working with more then 2 level xml hierarchy Pin
vgapp1-Apr-10 3:03
vgapp1-Apr-10 3:03 
GeneralLove this !!! Pin
cphelpsone22-Jan-10 7:57
cphelpsone22-Jan-10 7:57 
GeneralProblem with sub tabs and css Pin
maxitaxi9-Sep-09 8:40
maxitaxi9-Sep-09 8:40 
GeneralNow Tab Creation is very easy with Asp.net Pin
dilipsss21-May-09 21:40
dilipsss21-May-09 21:40 
An ASP.NET AJAX TabContainer creates a set of Tabs that can be used to save screen space and organize content. The TabContainer contains a number of TabPanel controls. You can place your controls inside each TabPanel. In this article, we will explore some common tips and tricks with the ASP.NET AJAX TabContainer control.
I assume you have some basic experience developing ASP.NET AJAX applications and have installed the ASP.NET AJAX Library and ASP.NET Control Toolkit. As of this writing, the toolkit version is Version 1.0.20229 (if you are targeting Framework 2.0, ASP.NET AJAX 1.0 and Visual Studio 2005) and Version 3.0.20229 (if targeting .NET Framework 3.5 and Visual Studio 2008).
All the tips shown below have been created using Version 3.0.20229 (targeting .NET Framework 3.5 and Visual Studio 2008).

Tip 1: How to Create a ASP.NET AJAX TabContainer with Tab Panels
Assuming you have AJAX Control Toolkit for 3.5 installed, Open Visual Studio 2008 > File > New Website > ‘ASP.NET WebSite’ > Enter a name and location for the project > Click Ok.
Drag and drop a <ScriptManager> from the Toolbox to the page. Now drag and drop a TabContainer to the page and using the smart tag, add a few tabpanels. Switch to the source view and add <ContentTemplates> inside each TabPanel. You can now place controls inside the <ContentTemplates>. The code will look similar to the following:
-------------------------------------------
Tab.aspx File
-------------------------------------------

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="cc1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Tab Container Tips & Tricks</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>

<cc1:TabContainer ID="TabContainer1" runat="server" ActiveTabIndex="0">

<cc1:TabPanel ID="TabPanel1" runat="server" HeaderText="TabPanel1">
<ContentTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
<asp:Button ID="Button1" runat="server" Text="Button" />
</ContentTemplate>
</cc1:TabPanel>

<cc1:TabPanel ID="TabPanel2" runat="server" HeaderText="TabPanel2">
<ContentTemplate>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />
<asp:Button ID="Button2" runat="server" Text="Button" />
</ContentTemplate>
</cc1:TabPanel>

<cc1:TabPanel ID="TabPanel3" runat="server" HeaderText="TabPanel3">
<ContentTemplate>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox><br />
<asp:Button ID="Button3" runat="server" Text="Button" />
</ContentTemplate>
</cc1:TabPanel>

</cc1:TabContainer>
<br />

</div>
</form>
</body>
</html>

---------------------------------------------
Tab.aspx.cs
----------------------------------------------

protected void btnLoop_Click(object sender, EventArgs e)
{
AjaxControlToolkit.TabContainer container = (AjaxControlToolkit.TabContainer)TabContainer1;
foreach (object obj in container.Controls)
{
if (obj is AjaxControlToolkit.TabPanel)
{
AjaxControlToolkit.TabPanel tabPanel = (AjaxControlToolkit.TabPanel)obj;
foreach (object ctrl in tabPanel.Controls)
{
if (ctrl is Control)
{
Control c = (Control)ctrl;
foreach (object innerCtrl in c.Controls)
{
if (innerCtrl is System.Web.UI.WebControls.TextBox)
Response.Write(((TextBox)innerCtrl).Text);
}
}
}
}
}

}

---------------------------------------------------
For VB.net use below code
-----------------------------------------------------

Protected Sub btnLoop_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim container As AjaxControlToolkit.TabContainer = CType(TabContainer1, AjaxControlToolkit.TabContainer)
For Each obj As Object In container.Controls
If TypeOf obj Is AjaxControlToolkit.TabPanel Then
Dim tabPanel As AjaxControlToolkit.TabPanel = CType(obj, AjaxControlToolkit.TabPanel)
For Each ctrl As Object In tabPanel.Controls
If TypeOf ctrl Is Control Then
Dim c As Control = CType(ctrl, Control)
For Each innerCtrl As Object In c.Controls
If TypeOf innerCtrl Is System.Web.UI.WebControls.TextBox Then
Response.Write((CType(innerCtrl, TextBox)).Text)
End If
Next innerCtrl
End If
Next ctrl
End If
Next obj

End Sub

IF any problem please let me know.
GeneralRe: Now Tab Creation is very easy with Asp.net Pin
vgapp22-May-09 0:30
vgapp22-May-09 0:30 
GeneralProblem to run the project Pin
dilipsss15-May-09 2:31
dilipsss15-May-09 2:31 
GeneralRe: Problem to run the project Pin
vgapp15-May-09 2:57
vgapp15-May-09 2:57 
GeneralRe: Problem to run the project Pin
dilipsss18-May-09 2:28
dilipsss18-May-09 2:28 
GeneralRe: Problem to run the project Pin
vgapp19-May-09 3:12
vgapp19-May-09 3:12 
GeneralRe: Problem to run the project Pin
Hariprasd_G16-Jul-09 0:45
Hariprasd_G16-Jul-09 0:45 
GeneralBuilding the tabstrip w/o xml Pin
Member 281399115-Oct-08 14:03
Member 281399115-Oct-08 14:03 
Generallink from other websites Pin
cristtiah14-Oct-08 10:34
cristtiah14-Oct-08 10:34 
GeneralCreate Tab Strip from database Pin
himanshu25615-Sep-08 21:58
himanshu25615-Sep-08 21:58 
GeneralRe: Create Tab Strip from database Pin
vgapp9-Sep-08 7:28
vgapp9-Sep-08 7:28 
GeneralCssClass help Pin
sun_rang7-Jul-08 12:47
sun_rang7-Jul-08 12:47 
GeneralRe: CssClass help Pin
vgapp8-Jul-08 4:41
vgapp8-Jul-08 4:41 
GeneralSetting ID in xml file Pin
sebsebserb14-Jun-08 16:24
sebsebserb14-Jun-08 16:24 
GeneralUsing Database Pin
heffen2-May-08 12:14
heffen2-May-08 12:14 
GeneralAJAX Version Pin
JohnnyHax25-Apr-08 14:44
JohnnyHax25-Apr-08 14:44 
GeneralRe: AJAX Version Pin
vgapp28-Apr-08 4:13
vgapp28-Apr-08 4:13 
GeneralRe: AJAX Version Pin
JohnnyHax28-Apr-08 19:41
JohnnyHax28-Apr-08 19:41 
QuestionAdd new tab Pin
Parthasarathy Mandayam5-Nov-07 7:21
Parthasarathy Mandayam5-Nov-07 7:21 

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.