Hierarchical Ajax Accordion + Databound

Posted on: Tue Jul 15 11:32:24 -0700 2008. Updated on: Wed Aug 13 11:41:01 -0700 2008.
Category: Asp .Net AJAX / Javascript / CSS

The accordion control from the ajax control toolkit can be pretty nice for building Hierarchical structures. The accordion provides expand and collapse to preserve screen real estate and keep the view organized.

Lets start with the aspx side: Drag in your controls and make em look like this:


                     
                        <%# Eval("Date", "{0:yyyy}") %> 
                     
                     
                         
                         
                            
                                <%# Eval("Date", "{0:MMMM}")%> 
                            
                            
                                
                                
                                
                            
                         
                         
                     
                    
                    

Notice a few things: In this example we are using a dates based hierarchy, starting out with the years and coming down to the months and finally to the days in that month.
The outer Accordion pane, Accordion1, is databound to an object datasource that puts out a table with a column "Date".
The inner accordion is not databound to anything, it will be databound in the codebehind. This is because we need to pull that value from the hiddenField that will let us know what year/month we need to build the next accordion pane for.
At the end we just have a GridView that lists the final days in the archive for the selected month.

Now for the css that provides the look, alter this to get something different than the default view provided by microsoft.

.accordionHeader
{
    border: 1px solid #2F4F4F;
    color: white;
    background-color: #2E4d7B;
	font-family: Arial, Sans-Serif;
	font-size: 12px;
	font-weight: bold;
    padding: 5px;
    margin-top: 5px;
    cursor: pointer;
}
.accordionHeader a
{
	color: #FFFFFF;
	background: none;
	text-decoration: none;
}
.accordionHeader a:hover
{
	background: none;
	text-decoration: underline;
}

.accordionHeaderSelected
{
    border: 1px solid #2F4F4F;
    color: white;
    background-color: #5078B3;
	font-family: Arial, Sans-Serif;
	font-size: 12px;
	font-weight: bold;
    padding: 5px;
    margin-top: 5px;
    cursor: pointer;
}
.accordionHeaderSelected a
{
	color: #FFFFFF;
	background: none;
	text-decoration: none;
}

.accordionHeaderSelected a:hover
{
	background: none;
	text-decoration: underline;
}

.accordionContent
{
    background-color: #D3DEEF;
    border: 1px dashed #2F4F4F;
    border-top: none;
    padding: 5px;
    padding-top: 10px;
}

Finally lets get to the codebehind, written in c-sharp of course:

protected void Accordion1_ItemDataBound(object sender, AjaxControlToolkit.AccordionItemEventArgs e)
    {
        IssuesData data = new IssuesData();

        if (e.ItemType == AjaxControlToolkit.AccordionItemType.Content)
        {
            AjaxControlToolkit.AccordionContentPanel contentPanel = e.AccordionItem;

            HiddenField yearHf = (HiddenField)contentPanel.FindControl("HiddenField1");

            AjaxControlToolkit.Accordion innerAccordion = (AjaxControlToolkit.Accordion)contentPanel.FindControl("AccordionMonths");
            innerAccordion.DataSource = data.LoadArchiveMonths(yearHf.Value);
            innerAccordion.DataBind();
        }


    }

    protected void AccordionMonths_ItemDataBound(object sender, AjaxControlToolkit.AccordionItemEventArgs e)
    {
        IssuesData data = new IssuesData();

        if (e.ItemType == AjaxControlToolkit.AccordionItemType.Content)
        {
            AjaxControlToolkit.AccordionContentPanel contentPanel = e.AccordionItem;

            HiddenField monthHf = (HiddenField)contentPanel.FindControl("HiddenFieldMonth");

            DateTime monthDate = Convert.ToDateTime(monthHf.Value);

            GridView datalist = (GridView)contentPanel.FindControl("GridViewDates");
            datalist.DataSource = data.LoadArchiveForMonth(monthDate.Month, monthDate.Year);
            datalist.DataBind();
        }
    
    }

Notice that these are the on row / item commands that get run during databinding. Note how we pull the hiddenfield value to then build the inner accordion pane with the right data, and further build the GridView with the right data.

Comments:

Sun Oct 26 14:54:34 -0700 2008
lcrnanga http://itmbuzed.com dhbzvzrc hkmgcnru <a href="http://cpbyaivh.com">rwbhdqdz</a> [URL=http://vrsocbhq.com]mugiqwpg[/URL]


---------------------------------------------------

Sun Oct 26 14:57:13 -0700 2008
[URL=http://bxmuwwiw.com]jospkoug[/URL] <a href="http://diccxdje.com">ciuzabbr</a> zhqhqxjg http://lvdoktre.com crcussnh aftjkuun


---------------------------------------------------

Sun Oct 26 14:58:42 -0700 2008
qevojsac http://vkuodwtc.com lcisxvxy fojhzmdv <a href="http://kquqjwyi.com">xlemvvyi</a> [URL=http://uvnybhlf.com]hbxlecpg[/URL]


---------------------------------------------------

Add a Comment:

simple_captcha.jpg
(human authentication)


Ads

Categories

About

Random foliage

This website is meant to be a reference for ASP Dot Net developers. The entries are a compilation of things I've figured out how to do and that I deem useful to keep of track for future reference. Assumptions: web development with: C Sharp (vb sucks), visual studio 05/08, .net 3.5, meant for programmers. Written by: James Reategui.