Nalaka526
Nalaka526

Reputation: 11464

Setting TabContainer Active Tab dynamically by visibility

I'm using AjaxControlToolkit TabContainer and dynamically setting TabPanel visiblility. When active tab is is hidden (dynamically) the whole Tab Control is hidden, so to avoid this I've written small method like this (setting first visible tab as Active Tab)

private void SetActiveTab()
{
    if (tabControl1.Tabs[0].Visible)
    {
        tabControl1.ActiveTabIndex =0;
        return;
    }

    if (tabControl1.Tabs[1].Visible)
    {
        tabControl1.ActiveTabIndex = 1;
        return;
    }

    ...

}

But this feels like but inefficient/ugly code for me..., Is there a better way of doing this...?

Upvotes: 0

Views: 4473

Answers (1)

IUnknown
IUnknown

Reputation: 22448

Choose any

for (int tabIndex = 0; tabIndex < TabContainer1.Tabs.Count; tabIndex++)
{
    if(TabContainer1.Tabs[tabIndex].Visible)
    {
        TabContainer1.ActiveTabIndex = tabIndex;
        break;
    }
}

foreach (TabPanel tab in TabContainer1.Tabs)
{
    if (tab.Visible)
    {
        TabContainer1.ActiveTab = tab;
        break;
    }
}

var firstVisibleTab = TabContainer1.Tabs.OfType<TabPanel>().FirstOrDefault(tab => tab.Visible);
if (firstVisibleTab != null)
{
    TabContainer1.ActiveTab = firstVisibleTab;
}

BTW for such quetions better use Code Review site: Code Review

Upvotes: 1

Related Questions