In my previous two blogs we saw some javascript tricks to work with ASP.NET AJAX controls. The links to my previous blogs are
In this blog we will start with a common requirement for the tab container control i.e. setting focus to a tab using javascript and later at the end of this blog we will also see some important javascript functions available with ASP.NET AJAX Tab Container control.
One of the coolest controls in the ASP.NET AJAX is the TabContainer. Recently I had the requirement to show controls in tabs and also if there was any validation error in any of the tab I need to set focus on that tab as well. So if you also have the same requirement like you wanted to set focus to a particular tab of ASP.NET Ajax TabContainer control using javascript then read on. Below is the html code for the page having a tab container and some other asp.net controls like the validation control, validator callout etc.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <body> |
So in the above html code you can see we have an AjaxControlToolkit TabContainer control with two TabPanels having id as “firstTab” and “secondTab”. The first tab has a label control and the second tab has a dropdown control with a validation control attached to it and a ValidatorCalloutExtender control attached to the validation control. Also we have a submit button with “OnClientClick” event calling a javscript function called “ValidatePage”. Now the problem is whenever the submit button is pressed and if there is a validation error in the second tab the user is not informed of the same, reason being he is in the first tab and error has happened in the second tab. When the user manually clicks on the second tab he is able to see the error and if he doesn’t click the second tab the user is oblivious of the error. The solution to the problem is to set focus on the second tab whenever there is a validation error in the second tab. The javascript solution for the same is pasted below.
<script language="javascript" type="text/javascript"> function hideValidatorCallout() function showValidatorCallout() |
If you see in the above code we are calling ValidatePage javascript method on the button’ OnClientClick event. Inside the function we are making a call to the ValidatorValidate method with the validation control as the argument. Once that is done I am checking whether the validation logic associated with validation control has executed successfully or not by checking the isvalid property of the validation control. If the validation is not successful I am getting the tab container control using the “$get” javascript method. Once the tab container is retrieved the following two lines sets the focus to the desired tab.
tabContainer = tabContainer.control; |
Once you retrieve the tab container object using the “control” property one can use “set_activeTabIndex” to set the focus to the required tab. The “set_activeTabIndex” method takes an integer (tab index) value as the argument. Using the “set_activeTabIndex” javascript method one can set focus to the required tab.
I have used a new javascript function called “$get” to retrieve an element. In my previous blog I have used “$find”, we will see the difference between these two in my next blog but for the time being just understand that “$get” is the short cut for “document.getElementById” javascript method.
Some other useful tab container control’ javascript functions
Below I have listed some of the important javscript functions which may be of use for developers like us. These methods can be used only after one has retrieved the ASP.NET AJAX TabContainer control using the following code “TABCONTAINER_CONTROL.control” as shown in the above codes.
Method | Explanation |
get_activeTab() | returns the current active tab javascript object i.e. the tab which is in focus. This javascript function gets the active tab object. |
get_activeTabIndex() | gets the active tab index. The function returns an integer value of the active tab. |
get_id() | gets the id of the tab container control. |
get_tabs() | gets an array of all the tabs in a tab container control. The function returns a javascript array containing all the tabs in the tab container control. |
get_visible() | returns a boolean value indicating whether the tab container is visible or not. |
getFirstTab() | gets the first tab in the tab container control. The function returns a tab having tab index 0. |
getLastTab() | gets the last tab in the tab container control. The function returns a tab object having tab index = (tab count) –1. |
getNearestTab() | gets the nearest tab. If the current tab index is 0 then the method will return second tab i.e. the tab having tab index as 1 and if the tab index is greater than 0 the function will return the previous tab i.e. the tab having index = [Current Tab Index] – 1. |
getNextTab() | gets the next tab in the tab container control i.e. if the current active tab is the last tab then the function will return the first tab as the next tab else it will return the next tab ([Current Tab Index] + 1) |
getPreviousTab() | gets the previous tab in the tab container control i.e. if the current tab index is 0 then the function returns the last tab else it returns the previous tab ([Current Tab Index] - 1) |
set_activeTab(tabControl) | sets the active tab. The function takes a tab object as its argument and sets the tab as the active tab. |
set_activeTabIndex(integer) | functions takes an integer value starting from 0 to tab collection length – 1 as an argument and sets the tab’ index matching the integer value as the active tab. |
set_id() | sets the id for the current tab container control. |
set_visible(Boolean) | function takes a Boolean value and based on the value, hides or shows the tab container. The function/property makes the whole tab container control visible or invisible. To make individual tabs visible or invisible you need to work with the tabs property as shown in the below table. |
Individual Tab object’ javascript functions
The above functions are related to the tab container control. There may situation where one might want to work with individual tab objects. To work with individual tabs one has to retrieve the individual tab by making us of any of the above tab retrieval functions from the table and then one can work with the individual tab. Sample e.g to retrieve individual tab is given below.
tabContainer = tabContainer.control; |
Once you have retrieved the individual tab using any of the above methods you can make use of some of the useful methods listed in the table below to work with the tab object. The methods listed in the below table are not the complete list but they are some of the methods which I feel may be useful for developers.
Methods | Explanation |
addCssClass(className) | sets the CSS class. The function takes the CSS class name as the argument. You can make use of this javascript function to change the CSS class of a tab at runtime through javascript. |
get_enabled() | gets a Boolean value indicating whether the tab is enabled or not (disabled). The function returns true or false. |
get_headerText() | gets the header text of the tab. The function returns string containing the header text. |
get_id() | gets the id of the tab. |
get_tabIndex() | gets the tab index of the tab object. The function returns an integer. |
get_visible() | gets whether the tab is visible or not. The function returns a boolean value. |
removeCssClass(className) | removes the CSS class based on the class name passed in the argument. |
set_enabled(Boolean) | enables or disables the tab object based on the boolean value passed. |
set_headerText(string) | function sets the header text for tab. Functions takes a string as an argument and sets the string as the header text. |
set_id() | sets the id for the tab. |
set_visible(Boolean) | sets the visibility of the tab based on the boolean argument passed. The boolean value makes the tab visible or invisible. |
In my next blog we will see the difference between $find and $get. Till then try to know more.
Sandeep.