Ajax Tab Container Extender in ASP.Net

The TabContainer is an AJAX Control to create a set of Tabs to organize the contents of an ASP.Net page. It is uesd to host a number of TabPanels in which we use a Text Header or HeaderTemplate as well as a ContentTemplate that defines its content.

 
Now we will see an example of an Ajax Tab Container extender in ASP.NET.
Step 1
 
First we will create a new project that is an ASP.NET WebApplication named AjaxTabContainer.
 
 
Now add one WebForm to this project named TabContainer.aspx.
 
 
 
 
Step 2
 
We will now open the Toolbox then select the Ajax Pane and drag ToolkitScriptManager to the div tag inside the WebForm.
 
 
 
Step 3
 
We will now select a TabContainer control from the ajax controls pane and add it after the ToolKitScriptManager.
 
 
 
 
 
Step 4
 
Now open the properties of the Tabcontainer and click the Add Tab Panels option.

It will create a new Tab Panels in our TabContainer control, here we will create two Tab Panels.
 
 
Now the source code will look like this.
 
 
For using Ajax Controls first we need to add an AjaxControlToolkit reference to our application.
  1. <%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="asp" %>  
We can use a TagPrefix also, for example:
 
tagPrefix="ajax", tagPrefix="cc1",tagPrefix="asp"
 
Step 5
 
Now we will change the HeaderText in the TabPanels using the following code. 
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TabContainer.aspx.cs" Inherits="AjaxTabContainer.TabContainer" %>  
  2.   
  3. <%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="asp" %>  
  4.   
  5. <!DOCTYPE html>  
  6.   
  7. <html xmlns="http://www.w3.org/1999/xhtml">  
  8. <head runat="server">  
  9.     <title></title>  
  10. </head>  
  11. <body>  
  12.     <form id="form1" runat="server">  
  13.     <div>  
  14.       
  15.         <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">  
  16.         </asp:ToolkitScriptManager>  
  17.         <br />  
  18.         <asp:TabContainer ID="TabContainer1" runat="server" ActiveTabIndex="1" Height="40px" Width="241px">  
  19.             <asp:TabPanel runat="server" HeaderText="Login Form" ID="TabPanel1">  
  20.             </asp:TabPanel>  
  21.             <asp:TabPanel ID="TabPanel2" runat="server" HeaderText="Registration Form">  
  22.             </asp:TabPanel>  
  23.         </asp:TabContainer>  
  24.       
  25.     </div>  
  26.     </form>  
  27. </body>  
  28. </html>  
It will look like this in the design view.
 
 
 
Now run the application and see the output.
 
 
Step 6
 
Now we want to design something in these Tab Panels. We will select the Tab Panel in source view then we will write <contentTamplate> </contentTamplate> tag.
Here for example we will design the Login page in the first panel using the following code. 
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TabContainer.aspx.cs" Inherits="AjaxTabContainer.TabContainer" %>  
  2.   
  3. <%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="asp" %>  
  4.   
  5. <!DOCTYPE html>  
  6.   
  7. <html xmlns="http://www.w3.org/1999/xhtml">  
  8. <head runat="server">  
  9.     <title></title>  
  10. </head>  
  11. <body>  
  12.     <form id="form1" runat="server">  
  13.     <div>  
  14.       
  15.         <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">  
  16.         </asp:ToolkitScriptManager>  
  17.         <br />  
  18.         <asp:TabContainer ID="TabContainer1" runat="server"     ActiveTabIndex="0" Height="165px" Width="350px">  
  19.             <asp:TabPanel runat="server" HeaderText="Login Form" ID="TabPanel1">  
  20.                 <ContentTemplate>  
  21.                     <asp:Label ID="lbllogin" runat="server" Text="Login Here" "True" "True" "True"  ForeColor="#660033"></asp:Label>  
  22.                     <table style="width: 100%;" border:"1">  
  23.                         <tr>  
  24.                             <td><asp:Label ID="lbluname" runat="server" Text="UserName" "True" ForeColor="#000099"></asp:Label>                             </td>  
  25.                             <td><asp:TextBox ID="tbuname" runat="server"></asp:TextBox></td>  
  26.                         </tr>  
  27.                         <tr>  
  28.                             <td><asp:Label ID="lblpass" runat="server" Text="Password" "True" ForeColor="#000099"></asp:Label>                             </td>  
  29.                             <td><asp:TextBox ID="tbpass" runat="server"></asp:TextBox></td>  
  30.                         </tr>  
  31.                         <tr>  
  32.                             <td> </td>  
  33.                             <td><asp:Button ID="btnSubmit" runat="server" Text="Submit" BackColor="#00CCFF" ForeColor="Blue" /></td>  
  34.                         </tr>  
  35.                     </table>   
  36.                 </ContentTemplate>  
  37.             </asp:TabPanel>  
  38.             <asp:TabPanel ID="TabPanel2" runat="server" HeaderText="Registration Form">  
  39.             </asp:TabPanel>  
  40.         </asp:TabContainer>  
  41.       
  42.     </div>  
  43.     </form>  
  44. </body>  
  45. </html>  
Now it looks like this in design view.
 
 
 
Now we will run the project and see the output for the Login form panel.
 
 
 
Step 7: Now for the second panel we will design the Registration page with the following code.

The following is the complete code for the Login and Registration Forms.
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TabContainer.aspx.cs" Inherits="AjaxTabContainer.TabContainer" %>  
  2.   
  3. <%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="asp" %>  
  4.   
  5. <!DOCTYPE html>  
  6.   
  7. <html xmlns="http://www.w3.org/1999/xhtml">  
  8. <head runat="server">  
  9.     <title></title>  
  10. </head>  
  11. <body>  
  12.     <form id="form1" runat="server">  
  13.     <div>  
  14.       
  15.         <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">  
  16.         </asp:ToolkitScriptManager>  
  17.         <br />  
  18.         <asp:TabContainer ID="TabContainer1" runat="server" ActiveTabIndex="1" Height="229px" Width="353px">  
  19.             <asp:TabPanel runat="server" HeaderText="Login Form" ID="TabPanel1">  
  20.                 <ContentTemplate>  
  21.                     <asp:Label ID="lbllogin" runat="server" Text="Login Here" "True" "True" "True" ForeColor="#660033"></asp:Label>  
  22.                     <table style="width: 100%;" border:"1">  
  23.                         <tr>  
  24.                             <td><asp:Label ID="lbluname" runat="server" Text="UserName" "True" ForeColor="#000099"></asp:Label></td>  
  25.                             <td><asp:TextBox ID="tbuname" runat="server"></asp:TextBox></td>  
  26.                         </tr>  
  27.                         <tr>  
  28.                             <td><asp:Label ID="lblpass" runat="server" Text="Password" "True" ForeColor="#000099"></asp:Label></td>  
  29.                             <td><asp:TextBox ID="tbpass" runat="server"></asp:TextBox></td>  
  30.                         </tr>  
  31.                         <tr>  
  32.                             <td> </td>  
  33.                             <td><asp:Button ID="btnSubmit" runat="server" Text="Submit" BackColor="#0099FF" "True" ForeColor="#000099" Height="27px" /></td>  
  34.                         </tr>  
  35.                     </table>   
  36.                 </ContentTemplate>  
  37.             </asp:TabPanel>  
  38.             <asp:TabPanel ID="TabPanel2" runat="server" HeaderText="Registration Form">  
  39.                 <ContentTemplate>  
  40.                     <asp:Label ID="lblRegistration" runat="server" Text="Registration Here" "True" "True" "True" ForeColor="#660033"></asp:Label>  
  41.                     <table style="width: 100%;" border:"1">  
  42.                         <tr>  
  43.                             <td><asp:Label ID="lblUserName" runat="server" Text="UserName" "True" ForeColor="#000099"></asp:Label></td>  
  44.                             <td><asp:TextBox ID="tbUserName" runat="server"></asp:TextBox></td>  
  45.                         </tr>  
  46.                         <tr>  
  47.                             <td><asp:Label ID="lblPassword" runat="server" Text="Password" "True" ForeColor="#000099"></asp:Label></td>  
  48.                             <td><asp:TextBox ID="tbPassword" runat="server"></asp:TextBox></td>  
  49.                         </tr>  
  50.                         <tr>  
  51.                             <td><asp:Label ID="lblFname" runat="server" Text="FirstName" "True" ForeColor="#000099"></asp:Label></td>  
  52.                             <td><asp:TextBox ID="tbFname" runat="server"></asp:TextBox></td>  
  53.                         </tr>  
  54.                         <tr>  
  55.                             <td><asp:Label ID="lblLastName" runat="server" Text="LastName" "True" ForeColor="#000099"></asp:Label></td>  
  56.                             <td><asp:TextBox ID="tbLastName" runat="server"></asp:TextBox></td>  
  57.                         </tr>  
  58.                         <tr>  
  59.                             <td> </td>  
  60.                             <td><asp:Button ID="btnsave" runat="server" Text="Save" BackColor="#0099FF" "True" ForeColor="#000099" Height="27px"/></td>  
  61.                         </tr>  
  62.                     </table>   
  63.                 </ContentTemplate>  
  64.             </asp:TabPanel>  
  65.         </asp:TabContainer>  
  66.        </div>  
  67.     </form>  
  68. </body>  
  69. </html>  
Now it is look like this in design view.
 
 
Now we will run the project and see the output for the Registration form panel.
 
 
Step 8: Now we will set some properties like AutoPostBack and BorderColor for the Tab Container.

The AutoPostBack is used when a tab is changed after a postback, we will get the same tab after postback.
 
 
It is all about how to use an Ajax Tab Container extender in ASP.NET.

Now we can provide some functionality for the submit button to collect all the information and bind it to a database.

Up Next
    Ebook Download
    View all
    Learn
    View all