I have an ASP.NET page that uses the ASP.NET Ajax Control Toolkit TabContainer. In the Page_Load event, I am hiding some of the tabs based on the data given to the page. I then want to make one of the tabs active based on the value of an (optional) query string parameter.
So I have:
protected void Page_Load ( object sender, EventArgs e )
{
if ( !this.IsPostBack )
{
// Tabs with no data are hidden in here
LoadDataIntoTabs();
PreselectCorrectTab();
}
}
private void PreselectCorrectTab ()
{
if ( ctlTabContainer.Visible )
{
if ( !string.IsNullOrEmpty( Request.QueryString[ "tabIndex" ] ) )
{
int tabIndex = 0;
if ( int.TryParse( Request.QueryString[ "tabIndex" ], out tabIndex ) )
{
if ( ( ctlTabContainer.Tabs.Count > tabIndex ) && ctlTabContainer.Tabs[ tabIndex ].Visible )
{
ctlTabContainer.ActiveTabIndex = tabIndex;
}
}
}
}
}
If I hit the page with the tabIndex query string parameter set, the entire tab container disappears.
The strange thing is that if I change LoadDataIntoTabs() to not hide tabs that contain no data, everything works as you would expect (i.e. the correct tab is selected when the page renders).
Any ideas?
EDIT
As requested, here are more details:
private void LoadDataIntoTabs ()
{
LoadPendingWidgetsTab();
LoadDataIntoTab2();
LoadDataIntoTab3();
// etc...
}
private void LoadPendingWidgetsTab ()
{
IList<Widget> pendingWidgets = GetAllPendingWidgets();
if ( ( pendingWidgets != null ) && ( pendingWidgets.Count > 0 ) )
{
tbpPendingWidgets.Visible = true;
tbpPendingWidgets.HeaderText = String.Format( "Pending Widgets ({0})", pendingWidgets.Count );
grdPendingWidgets.DataSource = pendingWidgets;
grdPendingWidgets.DataBind();
}
else
{
tbpPendingWidgets.Visible = false;
}
}
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Try to set the desired tab via ActiveTab like:
ctlTabContainer.ActiveTab = tbpPendingWidgets;
If you set the first tab to Visible=false then you have to set the next visible tab page via ActiveTab.
I’am using the AjaxControlToolkit Release 30930 (September 2009).
Method 2
This worked for me:
Manually resetting the index, the visibility and the active tab.
tabcontainer.ActiveTab = tabname tabcontainer.Visible = True tabcontainer.ActiveTabIndex = 2
In another situation where I was not trying to set the active tab, I had to reset tabcontainer.ActiveTabIndex = 0.
So I put the two together and it worked.
Method 3
this is simple and worked perfectly,try this
assign the tab index for every tab that are used in your tab container like….
then <cc1:TabContainer ID="TabContainer1" runat="server">
<cc1:TabPanel ID="tab1" runat="server" TabIndex="0">
//your panel
</cc1:TabPanel>
<cc1:TabPanel ID="tab2" runat="server" TabIndex="1">
//your panel
</cc1:TabPanel>
</cc1:TabContainer>
write this code in cs page
TabContainer1.ActiveTabIndex = 1;
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0