Selecting an AJAX AccordionPane by ID
Typically, if we want to programmatically select a particular AccordionPane within an ASP.NET AJAX Accordion control, we set the SelectedIndex property. This is great if we know the exact order of our AccordionPanes at all times, but this isn’t always so. Let’s say, for instance, that you have four panes (making their indices 0, 1, 2, and 3, respectively). If we make the third one (index 2) invisible, now the fourth one has an index of 2. So let’s create a method to select the AccordionPane by its ID instead.
VB.NET
Public Sub SetPane(ByVal acc As AjaxControlToolkit.Accordion, ByVal PaneID As String) Dim Index As Integer = 0 For Each pane As AjaxControlToolkit.AccordionPane In acc.Panes If (pane.Visible = True) Then If (pane.ID = PaneID) Then acc.SelectedIndex = Index Exit For End If Index += 1 End If Next End Sub
C#
protected void SetPane(AjaxControlToolkit.Accordion acc, string PaneID) { int Index = 0; foreach (AjaxControlToolkit.AccordionPane pane in acc.Panes) { if (pane.Visible == true) { if (pane.ID == PaneID) { acc.SelectedIndex = Index; break; } Index++; } } }
It is interesting to note that I attempted to create a client-side equivalent of this method. However, the ID is not passed down from the server to the client-side behavior, making it impossible to access that propery from the client.