6 Tips for Working with the ASP.NET AJAX Accordion Control


 <%@ 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>

Ebook Download
View all
Learn
View all