Hello,
I don't want to hardcode the indexes in my code to change between tabs. I would like to be able to look-up the index number for a tab, then set that tab. This allows me to change the order of my tabs without changing the navigation code.
There is an IndexOf(...) method in the TabItemCollection class. But this takes a reference to a ContentTabItem. However, there doesn't seem to be an ID property in the ContentTabItem, so there is no way that I can directly reference it.
As a workaround, here is the code that I have. Is there an easier way to do this?
private
int FindTabIndex(string tabText)
{
TabItemCollection tabs = this.WebTabMain.Tabs;
for (int i = 0; i < tabs.Count; i++)
if (tabs[i].Text == tabText)
return i;
}
throw new ArgumentOutOfRangeException("tabText");
I would prefer NOT to use the Text of the tab to determine this if possible.
HI Carl,
It is not clear what and how you want to find tab item. Tab item has the Index property, which returns its position within collection. There is also similar "property" tabItem.get_index() on client. So, if you want to select a particular tab item, then you may
this.WebTab1.SelectedIndex = tabItem.Index;
Hi Viktor,
Thank you for your reply.
I know that I can use the tabIndex to set the selected tab. The problem is finding the right tabItem.
Let's say that I have 3 tabs in my application: Cars, Trucks, and Planes. Let's say that an event occurs and I want to set the "Trucks" tab to be the active tab. I can just assume that the "Trucks" tab is in position 1 and say:
this.WebTab1.SelectedIndex = 1;
But then later if I decide to move the "Trucks" tab to position 2, then I must go and update this code.
When I declare the ContentTabItem in the aspx file, if I could add an ID to it, like "TrucksTab", then when I want to set that tab to be active I could use:
this.WebTab1.SelectedIndex = TrucksTab.Index;
In this case, it does not matter what position I move the tab to, it will always work.
Right now, in order to avoid hard coding the index, when I want to find the "Trucks" tab, I loop through the collection of tabs to find the one that has the text "Trucks." Then I set the selected index to the index of the tab that I found. It works for now, but it is a little clunky, and I also have to take into account localization.
I was wondering if I am missing another way to do this, or if maybe you would consider adding an ID field to the ContentTabItem in the next release?
Carli
Hi Carli,
The Index property should not change until you remove tab item from collection or insert new tab item in front of it.
The ID can not be used publically, because it is used for internally purposes.
If you still need to have some kind of identifier for tab item, then the best I can suggest is to use a property of WebControl like AccessKey. It is hidden by ContentPane (base class of tab item), but it should persist.
Thanks for your reply. I will continue to use the solution I am currently using.
Hello Carli,
Let we know if we can help further with this matter.
Regards.