AjaxToolkit: the last TabContainer on the page is focused on page load
ajaxcontroltoolkit, asp.net, tabcontainer
Solution
Place script below right after `ScriptManager` control:
<script type="text/javascript">
Sys.Extended.UI.TabContainer.prototype._app_onload = function (sender, e) {
if (this._cachedActiveTabIndex != -1) {
this.set_activeTabIndex(this._cachedActiveTabIndex);
this._cachedActiveTabIndex = -1;
var activeTab = this.get_tabs()[this._activeTabIndex];
if (activeTab) {
activeTab._wasLoaded = true;
//activeTab._setFocus(activeTab); -- disable focus on active tab in the last TabContainer
}
}
this._loaded = true;
}
</script>
Problem
I'm using more than one TabContainer on a page in an ASP.NET project and I noticed a really strange behavior: when the page is loaded the focus jumps to the last TabContainer on the page, causing it to scroll down. I don't explicitly focus on any control so I don't understand where this is coming from. I also switched places between the controls and it is always the last one that is focused. The TabContainers don't have any fancy settings, this is basically what they look like: ``` <cc1:TabContainer ID="tabContainer" runat="server"> <cc1:TabPanel runat="server" HeaderText="Header1" ID="tabPanel1" TabIndex="0"> <HeaderTemplate> <asp:Label ID="lblTab1" runat="server" Text="Tab1"></asp:Label> </HeaderTemplate> <ContentTemplate> ... (anything goes here, it still doesn't work) </ContentTemplate> </cc1:TabPanel> <cc1:TabPanel runat="server" HeaderText="Header2" ID="tabPanel2" TabIndex="1"> <HeaderTemplate> <asp:Label ID="lblTab2" EnableViewState="False" runat="server" Text="Tab2"></asp:Label> </HeaderTemplate> <ContentTemplate> ... (anything goes here, it still doesn't work) </ContentTemplate> </cc1:TabPanel> </cc1:TabContainer> ``` I know I can set focus on a control, I tried it but the page first scrolls to the tab container and then returns to the focused control (it doesn't look good). I tried this to set the focus to another control: ``` <body id="main" onload="javascript:document.getElementById('lnkLogout').focus();"> ``` Is this the standard behavior for the TabContainer? How can I get rid of it?