Click here to Skip to main content
15,881,027 members
Articles / Web Development / ASP.NET

Creating an AJAX Accordion Menu

Rate me:
Please Sign up or sign in to vote.
4.00/5 (1 vote)
25 May 2010CPOL3 min read 76.8K   3.6K   21   6
How to create an AJAX accordion menu.

Introduction

AJAX is a powerful addition to ASP.NET that provides new functionalities in a simple and agile way. This post is dedicated to creating a menu with the AJAX Accordion control.

About the control

The basic idea of this control is to provide a series of panels and show and hide information inside these panels. The use is very simple: we have to set each panel inside an Accordion control and give to each panel a header, and of course, we have to set the content of each panel.

To use accordion control, u need the ajax control toolkit.

Basic properties of the Accordion control

Before start developing using the Accordion control, we have to know the basic properties for this control:

AccordionPane.png

Other Accordion properties

  • FramesPerSecond - Number of frames per second used in the transition animations.
  • RequireOpenedPane - Prevents closing the currently opened pane when its header is clicked (which ensures one pane is always open). The default value is true.
  • SuppressHeaderPostbacks - Prevents the client-side click handlers of elements inside a header from firing (this is especially useful when you want to include hyperlinks in your headers for accessibility).
  • DataSource - The data source to use. DataBind() must be called.
  • DataSourceID - The ID of the data source to use.
  • DataMember - The member to bind to when using a DataSourceID.

AJAX Accordion Control Extender DataSource

The Accordion Control Extender of the AJAX Control Toolkit can also be used as a DataBound control. You can bind the data retrieved from the database to the Accordion control. The Accordion control consists of properties such as DataSource and DataSourceID (see above) that can be used to bind data. The HeaderTemplate can used to display the header or title for the pane generated by the Accordion control, a click on which will open or close the ContentTemplate generated by binding the data with the Accordion Extender.

When DataSource is passed to the Accordion control, we use the DataBind method to bind the data. The Accordion control bound with data auto generates the expand/collapse panes along with their headers.

This code represents the basic steps to bind the Accordion to a data source:

VB
Public Sub getCategories()
    Dim sqlConn As New SqlConnection(conString)
    sqlConn.Open()
    Dim sqlSelect As New SqlCommand("SELECT * FROM Categories", sqlConn)
    sqlSelect.CommandType = System.Data.CommandType.Text
    Dim sqlAdapter As New SqlDataAdapter(sqlSelect)
    Dim myDataset As New DataSet()
    sqlAdapter.Fill(myDataset)
    sqlConn.Close()

    Accordion1.DataSource = myDataset.Tables(0).DefaultView
    Accordion1.DataBind()

End Sub

Protected Sub Accordion1_ItemDataBound(sender As Object, _
          e As AjaxControlToolkit.AccordionItemEventArgs)
    If e.ItemType = AjaxControlToolkit.AccordionItemType.Content Then
        Dim sqlConn As New SqlConnection(conString)
        sqlConn.Open()
        Dim sqlSelect As New SqlCommand("SELECT productName " & _ 
            "FROM Products where categoryID = '" + _
            DirectCast(e.AccordionItem.FindControl("txt_categoryID"),_
            HiddenField).Value + "'", sqlConn)
        sqlSelect.CommandType = System.Data.CommandType.Text
        Dim sqlAdapter As New SqlDataAdapter(sqlSelect)
        Dim myDataset As New DataSet()
        sqlAdapter.Fill(myDataset)
        sqlConn.Close()

        Dim grd As New GridView()

        grd = DirectCast(e.AccordionItem.FindControl("GridView1"), GridView)
        grd.DataSource = myDataset
        grd.DataBind()
    End If
End Sub

In the above code, we did two things: first, we made a SQL select to a database to retrieve all the data from the Categories table. This data will be used to set the header and columns of the accordion.

ASP.NET
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>

<ajaxToolkit:Accordion
   ID="Accordion1" runat="server" TransitionDuration="100" 
   FramesPerSecond="200" FadeTransitions="true" RequireOpenedPane="false" 
   OnItemDataBound="Accordion1_ItemDataBound"
   ContentCssClass="acc-content" HeaderCssClass="acc-header" 
   HeaderSelectedCssClass="acc-selected">
    <HeaderTemplate>
      <%#DataBinder.Eval(Container.DataItem,"categoryName") %>
    </HeaderTemplate>
    <ContentTemplate>
      <asp:HiddenField ID="txt_categoryID" runat="server" 
          Value='<%#DataBinder.Eval(Container.DataItem,"categoryID") %>' 
     />
   <asp:GridView ID="GridView1" runat="server" 
         RowStyle-BackColor="#ededed" RowStyle-HorizontalAlign="Left"
         AutoGenerateColumns="false" GridLines="None" CellPadding="2" 
         CellSpacing="2" Width="300px">
<Columns>
   <asp:TemplateField 
       HeaderStyle-HorizontalAlign="Left" HeaderText="Product Name" 
       HeaderStyle-BackColor="#d1d1d1"
       HeaderStyle-ForeColor="#777777">

<ItemTemplate>
  <%#DataBinder.Eval(Container.DataItem,"productName") %>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
</ajaxToolkit:Accordion>

Here, we use <%#DataBinder.Eval(Container.DataItem,"categoryName") %> to bind the accordion header with the category name, so we made one header for each element found on the database.

Creating a basic accordion control

As we know, to use any of the AJAX components, there must be a registered ScriptManager in our site which will be responsible for managing our controls. So the first thing we will do is create our script manager.

ASP.NET
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager> 

Then we define our accordion element and establish some basic properties:

ASP.NET
<cc1:Accordion ID="AccordionCtrl" runat="server"
        SelectedIndex="0" HeaderCssClass="accordionHeader"
        ContentCssClass="accordionContent" AutoSize="None"
        FadeTransitions="true" TransitionDuration="250"
        FramesPerSecond="40"

For our work, we must declare Panes accordion inside it, these breads will be responsible for containing links or information that we want to show.

ASP.NET
<Panes>
    <cc1:AccordionPane ID="AccordionPane0" runat="server">
        <Header>Matenimiento</Header>
        <Content>
            <li><a href="mypagina.aspx">My página de prueba</a></li>
        </Content>
    </cc1:AccordionPane>

To end this work, we have to close all the panels and our accordion.

ASP.NET
</Panes>
</cc1:Accordion>

Finally, the finished example

ASP.NET
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>

<cc1:Accordion ID="AccordionCtrl" runat="server"
        SelectedIndex="0" HeaderCssClass="accordionHeader"
        ContentCssClass="accordionContent" AutoSize="None"
        FadeTransitions="true" TransitionDuration="250"
        FramesPerSecond="40">

    <Panes>
        <cc1:AccordionPane ID="AccordionPane0" runat="server">
            <Header>Matenimiento</Header>
            <Content>
                <li><a href="mypagina.aspx">My página de prueba</a></li>
            </Content>
        </cc1:AccordionPane>
    </Panes>
</cc1:Accordion>

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Systems Engineer
Costa Rica Costa Rica
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionBuenas tardes Pin
Sospecha12046811-May-15 13:09
Sospecha12046811-May-15 13:09 
QuestionEven more Dynamic? Pin
reg20065-Aug-10 22:02
reg20065-Aug-10 22:02 
GeneralGood Work Pin
Richard R. Fuller5-Jul-10 8:30
Richard R. Fuller5-Jul-10 8:30 
GeneralRe: Good Work Pin
jaullo128-Jul-10 16:39
jaullo128-Jul-10 16:39 
GeneralGood Job Pin
DecodedSolutions.co.uk25-May-10 4:36
DecodedSolutions.co.uk25-May-10 4:36 
GeneralSpelling Pin
david4life25-May-10 2:26
david4life25-May-10 2:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.