6 Tips for Working with the ASP.NET AJAX Accordion Control
As given in the ASP.NET AJAX Toolkit documentation “The Accordion is a web control that allows you to provide multiple panes and display them one at a time. It is like having several CollapsiblePanels where only one can be expanded at a time.” Here are some tips that could help you out in your projects while dealing with Accordion Control.
Before we start, the Accordion control on which we are applying these tips looks similar to the following:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Accordion Tips and Tricks</title>
<style type="text/css">
.accordionHeader
{
color: white;
background-color: #719DDB;
font: bold 11px auto "Trebuchet MS", Verdana;
font-size: 12px;
cursor: pointer;
padding: 4px;
margin-top: 3px;
}
.accordionContent
{
background-color: #DCE4F9;
font: normal 10px auto Verdana, Arial;
border: 1px gray;
padding: 4px;
padding-top: 7px;
}
</style>
<script type="text/javascript">
// Add the scripts shown in the tips over here
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<cc1:Accordion ID="AccordionCtrl" runat="server"
SelectedIndex="0" HeaderCssClass="accordionHeader"
ContentCssClass="accordionContent" AutoSize="None"
FadeTransitions="true">
<Panes>
<cc1:AccordionPane ID="AccordionPane0" runat="server">
<Header>Pane 1</Header>
<Content>
Lorem ipsum dolor sit amet, consectetur adipisicing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna
aliqua.
</Content>
</cc1:AccordionPane>
<cc1:AccordionPane ID="AccordionPane1" runat="server">
<Header>Pane 2</Header>
<Content>
Ut enim ad minim veniam, quis nostrud exercitation ullamco
laboris nisi ut aliquip ex ea commodo consequat.
</Content>
</cc1:AccordionPane>
<cc1:AccordionPane ID="AccordionPane2" runat="server">
<Header>Pane 3</Header>
<Content>
Duis aute irure dolor in reprehenderit in voluptate velit
esse cillum dolore eu fugiat nulla pariatur.
</Content>
</cc1:AccordionPane>
<cc1:AccordionPane ID="AccordionPane3" runat="server">
<Header>Pane 4</Header>
<Content>
Excepteur sint occaecat cupidatat non proident, sunt in
culpa qui officia deserunt mollit anim id est laborum.
</Content>
</cc1:AccordionPane>
</Panes>
</cc1:Accordion>
</div>
</form>
</body>
</html>
Tip 1: How to Add an ASP.NET AJAX Accordion Pane at Runtime
We can add a new Accordion Pane programmatically using both JavaScript as well as code-behind (C# or VB.NET)
Using Javascript
<script type="text/javascript">
function pageLoad()
{
AddPaneAtRuntime();
}
function AddPaneAtRuntime()
{
var newAccordion = $find("AccordionCtrl_AccordionExtender");
var header = document.createElement("div");
header.innerText = "Pane 5";
header.className = "accordionHeader"; //set header css
var content = document.createElement("div");
content.innerText = " This pane was added using JavaScript";
content.className = "accordionContent"; // set content css
newAccordion.get_element().appendChild(header);
newAccordion.get_element().appendChild(content);
newAccordion.addPane(header,content);
}
</script>
Using Codebehind
C#
protected void Page_Load(object sender, EventArgs e)
{
AccordionPane newAccordion = new AccordionPane();
newAccordion.HeaderContainer.Controls.Add(new LiteralControl("Pane 5"));
newAccordion.ContentContainer.Controls.Add(new
LiteralControl("This pane was added using Code Behind"));
AccordionCtrl.Panes.Add(newAccordion);
}
VB.NET
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
Dim newAccordion As New AccordionPane()
newAccordion.HeaderContainer.Controls.Add(New LiteralControl("Pane 5"))
newAccordion.ContentContainer.Controls.Add(New LiteralControl("This pane was added using Code Behind"))
AccordionCtrl.Panes.Add(newAccordion)
End Sub
Tip 2: How to Hide an ASP.NET AJAX Accordion Pane at Runtime
In order to hide an Accordion Pane, use the following script:
<script type="text/javascript">
function pageLoad()
{
hideAccordionPane(1);
}
// hides pane 1
function hideAccordionPane(paneno)
{
$find('AccordionCtrl_AccordionExtender').get_Pane(paneno).header.style.display="none";
$find('AccordionCtrl_AccordionExtender').get_Pane(paneno).content.style.display="none";
}
</script>
Tip 3: How to programmatically expand an ASP.NET AJAX Accordion Pane
In order to programmatically expand a given Accordion Pane, use the set_SelectedIndex on the Accordion control. The sample shown below expands Pane 3 programmatically
<script type="text/javascript">
function pageLoad()
{
changeSelected(2); // expand pane 3
}
// expand given accordion pane
function changeSelected(idx)
{
$find('AccordionCtrl_AccordionExtender').set_SelectedIndex(idx);
}
</script>
Tip 4: How to determine when an ASP.NET AJAX Accordion Pane has been expanded/selected
In order to determine when the user selects a different pane, you would need to add the add_selectedIndexChanged event to the Accordion and then determine the selected pane using get_SelectedIndex()
<script type="text/javascript">
function pageLoad()
{
var accCtrl = $find('AccordionCtrl_AccordionExtender');
accCtrl.add_selectedIndexChanged(onAccordionPaneChanged);
}
function onAccordionPaneChanged(sender, eventArgs)
{
var selPane = sender.get_SelectedIndex() + 1;
alert('You selected Pane ' + selPane);
}
</script>
I got this tip from the asp.net forums and was written by JonathanShen
Tip 5: Expand an ASP.NET AJAX Accordion Pane on MouseOver
By default, the Accordion Pane expands on the Click event. If you want to override this functionality and expand it on mouseover, use this script
<script type="text/javascript">
function pageLoad()
{
AddMouseOverToAccordion();
}
function AddMouseOverToAccordion()
{
var acc = $find('AccordionCtrl_AccordionExtender');
for(paneIdx = 0; paneIdx < acc.get_Count(); paneIdx++)
{
$addHandler(acc.get_Pane(paneIdx).header,"mouseover",acc._headerClickHandler);
}
}
</script>
Tip 6: Prevent an ASP.NET AJAX Accordion Pane from Expanding
This tip is helpful if you want to restrict the view of an Accordion Pane. Use the following script to prevent users from expanding Pane 3
<script type="text/javascript">
function pageLoad()
{
RemoveHandlerAtRuntime(2);
}
function RemoveHandlerAtRuntime(pane)
{
$removeHandler($find('AccordionCtrl_AccordionExtender').get_Pane(pane).header,"click",$find('AccordionCtrl_AccordionExtender')._headerClickHandler);
}
</script>
I hope those tips were helpful. If you have got one that has not been covered here, then write back and share your tips with the other readers. I hope this article was useful and I thank you for viewing it
The source code for this article can be downloaded over here.
This article has been editorially reviewed by Suprotim Agarwal.
C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.
We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).
Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.
Click here to Explore the Table of Contents or Download Sample Chapters!
Was this article worth reading? Share it with fellow developers too. Thanks!
Suprotim Agarwal, MCSD, MCAD, MCDBA, MCSE, is the founder of
DotNetCurry,
DNC Magazine for Developers,
SQLServerCurry and
DevCurry. He has also authored a couple of books
51 Recipes using jQuery with ASP.NET Controls and
The Absolutely Awesome jQuery CookBook.
Suprotim received the prestigious Microsoft MVP award for 17 consecutive years, until he resigned from the program in 2025. In a professional capacity, he is the CEO of A2Z Knowledge Visuals Pvt Ltd, a digital group that offers Digital Marketing and Branding services to businesses, both in a start-up and enterprise environment.
Get in touch with him on Twitter @suprotimagarwal or at LinkedIn