2. The Accordion
The Accordion is a
container that stacks several panels on top of one another, and allows
you to view one at a time. Each panel has a header (which usually
displays a section title) and some content. When you click the header of
one of the panels, that panel is expanded and the other panels are
collapsed, leaving just their headers visible. Figure 2 demonstrates the effect you'll see as you click different headers.
It goes without saying that
the collapsing behavior happens without a full-page postback. In fact,
there's no need to contact the web server at all. The first time the
page is generated, all the panels are rendered to HTML, but they're
hidden using CSS style attributes. When you click a header, a JavaScript
routine runs and changes these style attributes. (In truth, the
Accordion control is a bit more sophisticated than that. When you choose
a new panel, it gradually expands
into view, which is much more impressive than simply popping into
existence in one step. Furthermore, you can set the FadeTransitions
property to True if you want panels to fade into and out of sight when
you change from one panel to another.)
The Accordion control contains a collection of AccordionPane controls.
Each AccordionPane represents a separate panel in the Accordion.
Here's an example that illustrates this structure by putting two AccordionPane objects inside the Accordion:
<asp:Accordion ID="Accordion1" runat="server">
<Panes>
<asp:AccordionPane runat="server">
...
</asp:AccordionPane>
<asp:AccordionPane runat="server">
...
</asp:AccordionPane>
</Panes>
</asp:Accordion>
To determine what
AccordionPane is currently visible (or to set it), you use the
Accordion.SelectedIndex property. If RequiredOpenedPane is True, there
will always be at least one expanded panel. If it's False, you can
collapse all the panels—just click the header of the currently expanded
section (or set the SelectedIndex property to −1 in your code).
|
|
Each AccordionPane
consists of two sections. The Header section is used for the clickable
header of the panel, while the Content holds the details inside.
<asp:Accordion ID="Accordion1" runat="server"
HeaderCssClass="accordionHeader"
HeaderSelectedCssClass="accordionHeaderSelected"
ContentCssClass="accordionContent">
<Panes>
<asp:AccordionPane ID="paneColors" runat="server">
<Header>Colors</Header>
<Content>
Choose a background color:<br />
<asp:DropDownList ID="lstBackColor" runat="server"
Width="194px" AutoPostBack="True">
</asp:DropDownList>
<br /><br />
Choose a foreground (text) color:<br />
<asp:DropDownList ID="lstForeColor" runat="server"
Height="22px" Width="194px" AutoPostBack="True" >
</asp:DropDownList>
</Content>
</asp:AccordionPane>
<asp:AccordionPane ID="paneText" runat="server">
<Header>Text</Header>
<Content>
Choose a font name:<br />
<asp:DropDownList ID="lstFontName" runat="server"
Height="22px" Width="194px" AutoPostBack="True">
</asp:DropDownList>
<br /><br />
Specify a font size:<br />
<asp:TextBox ID="txtFontSize" runat="server"
AutoPostBack="True">
</asp:TextBox>
<br /><br />
Enter the greeting text below:<br />
<asp:TextBox ID="txtGreeting" runat="server"
Height="85px" Width="240px" TextMode="MultiLine"
AutoPostBack="True">
</asp:TextBox>
</Content>
</asp:AccordionPane>
<asp:AccordionPane ID="paneExtras" runat="server">
<Header>Other</Header>
<Content>
Choose a border style:<br />
<asp:RadioButtonList ID="lstBorder" runat="server"
Height="59px" Width="177px" Font-Size="X-Small"
AutoPostBack="True" RepeatColumns="2">
</asp:RadioButtonList>
<br /><br />
<asp:CheckBox ID="chkPicture" runat="server"
Text="Add the Default Picture" AutoPostBack="True">
</asp:CheckBox>
</Content>
</asp:AccordionPane>
</Panes>
</asp:Accordion>
Along with the
content, this example adds three properties: HeaderCssClass,
HeaderSelectedCssClass, and ContentCssClass. These properties take the
names of CSS styles that the Accordion uses to format the appropriate
region. The styles are defined in a separate style sheet, and look like
this:
.accordionHeader
{
border: 1px solid #2F4F4F;
color: white;
background-color: #2E4d7B;
padding: 5px;
margin-top: 5px;
cursor: pointer;
}
.accordionHeaderSelected
{
border: 1px solid #2F4F4F;
color: white;
background-color: #5078B3;
padding: 5px;
margin-top: 5px;
cursor: pointer;
}
.accordionContent
{
background-color: #D3DEEF;
border: 1px dashed;
border-top: none;
padding: 5px;
}
You don't need to use them with the
Accordion—after all, you could just set the formatting properties of
your controls, or wrap each separate section in a formatted Panel or
<div> element—but the style approach is pretty convenient once you
get used to it.
You can really fine-tune
the display that the Accordion uses by setting a few more properties.
TransitionDuration sets the number of milliseconds that the collapsing
and expanding animation lasts. FramesPerSecond controls how smooth the
transition animation is—a higher value produces better quality, but
requires more work from the browser. Finally, AutoSize lets you control
how the Accordion expands when you show a panel with a large amount of
content. Use a value of None to let the Accordion grow as large as it
needs (in which case other content underneath the Accordion is simply
bumped out of the way). Use Limit or Fill to restrict the Accordion to
whatever you've set in the Height property (the difference is the Limit
allows the Accordion to shrink smaller, while Fill keeps it at the
maximum height by leaving any unused space blank). With Limit or Fill,
the Accordion panels will use scrolling if they can't fit all their
content into the available space.
Clearly, the Accordion is a
simple-to-use, yet impressive way to deal with dense displays of data
and groups of information. If you want to have a similar collapsing and
expanding effect with a single panel, you might want to try another one
of the components in the ASP.NET AJAX Control Toolkit—the
CollapsiblePanelExtender.