JavaScript methods for controlling ASP.NET AJAX Accordion panels programmatically

I’m working with an ASP.NET AJAX Accordion control and need to manipulate it through JavaScript based on user actions. I want to read and modify the active panel, but my current approach isn’t working properly.

Here’s what I’ve tried so far:

function handleAccordionControl() {
  var accordionElement = $get('ctl00_MainAccordion_ExtenderControl_State');
  currentPanel = accordionElement.get_ActivePanel(); // this fails
}

The error occurs when I try to access the accordion’s current state. I’m looking for the correct way to interact with the accordion extender using client-side JavaScript. What’s the proper method to get and set the selected accordion pane programmatically?

i had the same prob! using $find() instead worked for me too. just remember to use the accordion ID, like this: var accordion = $find('ctl00_MainAccordion'); then u can control the panels easily!

The problem is ASP.NET changes the IDs you set in markup when it renders controls. Don’t hardcode that messy client ID like ‘ctl00_MainAccordion_ExtenderControl_State’ - use the ClientID property instead.

Expose the client ID in your code-behind:

protected string AccordionClientID
{
    get { return MainAccordion.ClientID; }
}

Then use it in JavaScript:

function handleAccordionControl() {
    var accordion = $find('<%= AccordionClientID %>');
    if (accordion != null) {
        var activePanel = accordion.get_SelectedIndex();
        accordion.set_SelectedIndex(activePanel + 1); // Move to next panel
    }
}

This fixes those annoying ID mismatches that break accordion controls. Also make sure EnablePartialRendering is set to true on your ScriptManager.

You’re calling accordion methods on a regular DOM element - that won’t work. $get() returns a standard HTML element, not the accordion extender object. You need the accordion’s client-side behavior instead.

Try this:

function handleAccordionControl() {
    var accordion = $find('MainAccordion'); // Use the actual accordion ID
    if (accordion) {
        var currentIndex = accordion.get_SelectedIndex();
        // To set a different panel active:
        accordion.set_SelectedIndex(2); // Opens panel at index 2
    }
}

Make sure this runs after the page loads completely, or the accordion object might not be ready yet. I wrap it in a pageLoad function to get the timing right.