Create new account I forgot my password    

ASP.NET AJAX CollapsiblePanelExtender - Tips and Tricks
Rating: 11 user(s) have rated this article Average rating: 4.6
Posted by: Suprotim Agarwal, on 11/20/2008, in category "ASP.NET AJAX"
Views: this article has been read 70095 times
Abstract: As given in the ASP.NET AJAX toolkit documentation, the CollapsiblePanel is a very flexible extender control that allows you to easily add collapsible sections to your web page. In this article, we will see six tips while working with the CollapsiblePanelExtender

ASP.NET AJAX CollapsiblePanelExtender - Tips and Tricks

As given in the ASP.NET AJAX toolkit documentation, the “CollapsiblePanel is a very flexible extender that allows you to easily add collapsible sections to your web page. This extender targets any ASP.NET Panel control” Here are some tips that will help you out in your projects while dealing with CollapsiblePanel Control.

I assume you have some basic experience developing ASP.NET AJAX applications and have installed the ASP.NET AJAX Library and ASP.NET Control Toolkit. As of this writing, the toolkit version is Version 1.0.20229 (if you are targeting Framework 2.0, ASP.NET AJAX 1.0 and Visual Studio 2005) and Version 3.5.20229 (if targeting .NET Framework 3.5 and Visual Studio 2008). Read more about it over here
To apply these tips, I have created a sample with the CollapsiblePanel control. Assuming you have AJAX Control Toolkit for 3.5 installed, Open Visual Studio 2008 > File > New Website > ‘ASP.NET WebSite’ > Enter a name and location for the project > Click Ok.
Drag and drop a <ScriptManager> from the Toolbox to the page. Now add an UpdatePanel to the page. Inside the <ContentTemplate> drag and drop a CollapsiblePanel from the AJAX toolkit. After applying some Css and add properties of the CollapsiblePanel, the mark up will look 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>CollapsiblePanelExtender Tips </title>   
    <style type="text/css">
        .cpHeader
        {
            color: white;
            background-color: #719DDB;
            font: bold 11px auto "Trebuchet MS", Verdana;
            font-size: 12px;
            cursor: pointer;
            width:450px;
            height:18px;
            padding: 4px;           
        }
        .cpBody
        {
            background-color: #DCE4F9;
            font: normal 11px auto Verdana, Arial;
            border: 1px gray;               
            width:450px;
            padding: 4px;
            padding-top: 7px;
        }      
    </style>
   
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            <asp:Panel ID="pHeader" runat="server" CssClass="cpHeader">
                <asp:Label ID="lblText" runat="server" />
            </asp:Panel>
 
            <asp:Panel ID="pBody" runat="server" CssClass="cpBody">
            Lorem ipsum dolor sit amet, consectetur adipisicing elit,
            sed do eiusmod tempor incididunt ut labore et dolore magna
aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur
            </asp:Panel>
 
<cc1:CollapsiblePanelExtender ID="CollapsiblePanelExtender1" runat="server" TargetControlID="pBody" CollapseControlID="pHeader" ExpandControlID="pHeader"
Collapsed="true" TextLabelID="lblText" CollapsedText="Click to Show Content.." ExpandedText="Click to Hide Content.."
CollapsedSize="0">
            </cc1:CollapsiblePanelExtender>
           
        </ContentTemplate>
        </asp:UpdatePanel>
    </div>
    </form>
</body>
</html>
 
Tip 1: How to Expand/Collapse the ASP.NET AJAX CollapsiblePanel programmatically using JavaScript?
In order to expand/collapse the CollapsiblePanel programmatically, use the following code. In this sample, we will be expanding/collapsing the CollapsiblePanel on a button click. The button is kept outside the CollapsiblePanel but inside the UpdatePanel.
...
<asp:Button ID="btnClick" OnClientClick="ExpandCollapse()"
runat="server" Text="Expand/Collapse" />
</ContentTemplate>
</asp:UpdatePanel>
Add JavaScript in the <head> tag as shown below:
    <script type="text/javascript">
 
        function ExpandCollapse() {           
            var collPanel = $find("CollapsiblePanelExtender1");
            if (collPanel.get_Collapsed())
                collPanel.set_Collapsed(false);
            else
                collPanel.set_Collapsed(true);
        }      
 
    </script>
Tip 2: How to Expand/Collapse the ASP.NET AJAX CollapsiblePanel programmatically using C# or VB.NET?
Set the ‘Collapsed’ property to true (collapses panel) or false (expands panel). Alternatively, setting the ‘ClientState’ to true (collapses panel) or false (expands panel) does the trick.
C#
    protected void btn_Collapse(object sender, EventArgs e)
    {
        // Expand
        this.CollapsiblePanelExtender1.Collapsed = false;
        this.CollapsiblePanelExtender1.ClientState = "false";
        // Collapse
        // Expand
        this.CollapsiblePanelExtender1.Collapsed = true;
        this.CollapsiblePanelExtender1.ClientState = "true";
    }
 
VB.NET
 
      Protected Sub btn_Collapse(ByVal sender As Object, ByVal e As EventArgs)
            ' Expand
            Me.CollapsiblePanelExtender1.Collapsed = False
            Me.CollapsiblePanelExtender1.ClientState = "false"
            ' Collapse
            ' Expand
            Me.CollapsiblePanelExtender1.Collapsed = True
            Me.CollapsiblePanelExtender1.ClientState = "true"
      End Sub
Tip 3: How to prevent flickering to occur when the ASP.NET AJAX Collapsible Panel first loads?
At Page Load, if the CollapsiblePanel expands and then collapses again causing a flashing to occur, you can solve it by setting the height of the Panel body to 0 and setting the overflow to hidden
        .cpBody
        {
            background-color: #DCE4F9;
            font: normal 11px auto Verdana, Arial;
            border: 1px gray;               
            width:450px;           
            padding: 4px;
            padding-top: 2px;
            height:0px;
          overflow: hidden;
        }
Tip 4: How to make an ASP.NET AJAX CollapsiblePanelExtender behave like an Accordion control?
Accordion allows you to have multiple panes where only one can be expanded at a time. When one of the panels is expanded by the user, the other collapses. Let us see how to achieve this functionality in the CollapsiblePanelExtender. I got this tip from Vince Xu - MSFT.
Add two Panels and corresponding CollapsiblePanelExtenders for the two Panels as shown below:
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            <asp:Panel ID="pHeader" runat="server" CssClass="cpHeader">
                <asp:Label ID="lblText" runat="server" />
            </asp:Panel>
            <asp:Panel ID="pBody" runat="server" CssClass="cpBody" >
            Lorem ipsum dolor sit amet, consectetur adipisicing elit,
            sed do eiusmod tempor incididunt ut labore et dolore magna
aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur
            </asp:Panel>
<cc1:CollapsiblePanelExtender ID="CollapsiblePanelExtender1" runat="server" TargetControlID="pBody" CollapseControlID="pHeader" ExpandControlID="pHeader"
Collapsed="false" TextLabelID="lblText" CollapsedText="Click to Show Content.." ExpandedText="Click to Hide Content.." CollapsedSize="0">
            </cc1:CollapsiblePanelExtender>           
          
            <asp:Panel ID="pHeaderTwo" runat="server" CssClass="cpHeader">
                <asp:Label ID="lblTextTwo" runat="server" />
            </asp:Panel>
            <asp:Panel ID="pBodyTwo" runat="server" CssClass="cpBody" >
            This is my second panel
            </asp:Panel>
           
<cc1:CollapsiblePanelExtender ID="CollapsiblePanelExtender2" runat="server" TargetControlID="pBodyTwo" CollapseControlID="pHeaderTwo" ExpandControlID="pHeaderTwo"
Collapsed="true" TextLabelID="lblTextTwo" CollapsedText="Click to Show Content.." ExpandedText="Click to Hide Content.." CollapsedSize="0">
            </cc1:CollapsiblePanelExtender>
          
        </ContentTemplate>
        </asp:UpdatePanel>
Now add some JavaScript in the <head> as shown below:
    <script type="text/javascript">
 
        function pageLoad(sender, args) {           
            for (num = 1; num < 3; num++) {
                $find("CollapsiblePanelExtender" + num).add_expandComplete(coll_ExpandedComplete);               
            }
        }
 
        function coll_ExpandedComplete(sender, arg) {           
            for (num = 1; num < 3; num++) {
var CollapsiblePanel = $find("CollapsiblePanelExtender" + num);
if (sender._expandControlID != CollapsiblePanel._expandControlID)
                    CollapsiblePanel.collapsePanel(CollapsiblePanel._expandControlID);
            }          
        }               
       
    </script>  
Note: Observe that the first Panel is expanded on load.
Tip 5: How to add an ASP.NET AJAX CollapsiblePanelExtender dynamically?
Use the following code to create and add a CollapsiblePanelExtender dynamically
C#
using AjaxControlToolkit;
 
    protected void Page_Load(object sender, EventArgs e)
    {        
        // Create Header Panel
        Panel panelHead = new Panel();
        panelHead.ID = "pH";
        panelHead.CssClass = "cpHeader";
        // Add Label inside header panel to display text
        Label lblHead = new Label();
        lblHead.ID = "lblHeader";
        panelHead.Controls.Add(lblHead);
 
        //Create Body Panel
        Panel panelBody = new Panel();
        panelBody.ID = "pB";
        panelBody.CssClass = "cpBody";
        // Add Label inside body Panel to display text
        Label lblB = new Label();
        lblB.ID = "lblBody";
        lblB.Text = "This panel was added dynamically";
        panelBody.Controls.Add(lblB);
 
        // Create CollapsiblePanelExtender
        CollapsiblePanelExtender cpe =
            new CollapsiblePanelExtender();
        cpe.TargetControlID = panelBody.ID;
        cpe.ExpandControlID = panelHead.ID;
        cpe.CollapseControlID = panelHead.ID;
        cpe.ScrollContents = false;
        cpe.Collapsed = true;
        cpe.ExpandDirection =
        CollapsiblePanelExpandDirection.Vertical;
        cpe.SuppressPostBack = true;
        cpe.TextLabelID = lblHead.ID;
        cpe.CollapsedText = "Click to Show Content..";
        cpe.ExpandedText = "Click to Hide Content..";
 
        this.UpdatePanel1.ContentTemplateContainer.Controls.Add(panelHead);
        this.UpdatePanel1.ContentTemplateContainer.Controls.Add(panelBody);
        this.UpdatePanel1.ContentTemplateContainer.Controls.Add(cpe);
 
    }
 
VB.NET
Imports AjaxControlToolkit
 
      Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
            ' Create Header Panel
            Dim panelHead As New Panel()
            panelHead.ID = "pH"
            panelHead.CssClass = "cpHeader"
            ' Add Label inside header panel to display text
            Dim lblHead As New Label()
            lblHead.ID = "lblHeader"
            panelHead.Controls.Add(lblHead)
 
            'Create Body Panel
            Dim panelBody As New Panel()
            panelBody.ID = "pB"
            panelBody.CssClass = "cpBody"
            ' Add Label inside body Panel to display text
            Dim lblB As New Label()
            lblB.ID = "lblBody"
            lblB.Text = "This panel was added dynamically"
            panelBody.Controls.Add(lblB)
 
            ' Create CollapsiblePanelExtender
            Dim cpe As New CollapsiblePanelExtender()
            cpe.TargetControlID = panelBody.ID
            cpe.ExpandControlID = panelHead.ID
            cpe.CollapseControlID = panelHead.ID
            cpe.ScrollContents = False
            cpe.Collapsed = True
            cpe.ExpandDirection = CollapsiblePanelExpandDirection.Vertical
            cpe.SuppressPostBack = True
            cpe.TextLabelID = lblHead.ID
            cpe.CollapsedText = "Click to Show Content.."
            cpe.ExpandedText = "Click to Hide Content.."
 
            Me.UpdatePanel1.ContentTemplateContainer.Controls.Add(panelHead)
            Me.UpdatePanel1.ContentTemplateContainer.Controls.Add(panelBody)
            Me.UpdatePanel1.ContentTemplateContainer.Controls.Add(cpe)
 
      End Sub
 
Tip 6: How to create a Smooth Animation while Expanding/Collapsing the ASP.NET AJAX CollapsiblePanelExtender?
To create a smooth animation while expanding/collapsing panels, change the fps and duration property using client script as shown below:
<script type="text/javascript">
 
        function pageLoad(sender, args) {
            smoothAnimation();
        }
       
       
    function smoothAnimation()
    {
            var collPanel = $find("CollapsiblePanelExtender1");
            collPanel._animation._fps = 45;
            collPanel._animation._duration = 0.90;
    }
 
</script>  
Well those were some tips associated with the AJAX CollapsiblePanelExtender. If you have a tip about this control not covered in this article, share the via the comments section. I hope this article was useful and I thank you for viewing it.
If you liked the article,  Subscribe to the RSS Feed or Subscribe Via Email  









Page copy protected against web site content infringement by Copyscape


How would you rate this article?

User Feedback
Comment posted by Rakesh Khambra on Wednesday, November 25, 2009 5:11 AM
First of I would like to say THANKS reason being I have seen lot of sites to understand the code of .net but the method to understanding of this site is different to others. That is good site to get code and implement the code. Thanks
Comment posted by Shaunny on Tuesday, December 15, 2009 6:23 AM
Thanks for the article; the explanation was very clear and useful. I do have one question, which is how does one expand/collapse the panel with smooth animation server side, or is tip 6 suppose to cater for this?

Thanks again!
Comment posted by Shaunny on Tuesday, December 15, 2009 7:59 AM
PS. I solved my problem with the pageLoad running behind the server script inserts (ScriptManager.RegisterStartupScript) by delaying the script by a millisecond with a setTimeout callback. Not the most eligent of solution, but does the trick.
Comment posted by mcdc on Tuesday, December 22, 2009 4:04 PM
just want I was looking for.  thanks for posting.
Comment posted by tpriede on Friday, December 25, 2009 2:08 PM
Hello,
This article is really good and helpfull. Personally, I'm not very experienced on AJAX control and I was looking for something clear regarding collapsible panels.
However, when trying your example there is something that doesn't work: the expand action
Comment posted by tpriede on Friday, December 25, 2009 2:09 PM
Hello,
This article is really good and helpful. Personally, I'm not very experienced with AJAX controls and I was looking for something clear regarding collapsible panels. Your article is.
However, when trying your example, there is something that doesn't work: the expand action. The panel does not expands neither by clicking on the header panel nor by clicking on the Collapse/Expand button, regardless it’s done with JavaScript or C#. In order to test if the panel was working I have set the Collapse property to False so the panel opens expanded and it does. When I click on the header panel or on the C# button, the panel collapse, but then it does not expand again on any action.

I have created the page with the exact code published in the article. Could you please help me find why the expand action doesn’t work?

Besides, I have another question. Is it possible to place controls like radio buttons or List Boxes, etc. on a collapsible panel? If not, is there any other AJAX control where I can show and hide controls as part of a content region/panel?

Thank you in advance.
Antonio Priede
Comment posted by Jack on Thursday, February 18, 2010 5:37 PM
Some of the tips are useful, except Tip #1.  How do you implement get_Collapsed() and set_Collapsed()?
Comment posted by Ivelin Andreev (Interconsult Bulgaria Ltd.) on Thursday, March 11, 2010 10:00 AM
I found this article while trying to spot more elegant way for eliminating flickering.
I have the following remarks:
The tip for flickering will NOT work. The proposed solution WILL NOT flicker for closed panels BUT WILL for open panels. To fix this use the following:
1. use ClientState property of the extender to determine on the server whether the panel is collapsed or not (Collapsed property will not show what has happened on the client)
2. for the extenders that are collapsed use TargetControlID to dynamically search in the extender parent for the specific panel (or other control type that you may use)
3. for the target control apply inline style height and set it to 0px (if it is collapsed), otherwise set it to empty string

The code could be similar to the one below:
bool collapsed = bool.Parse(cpe.ClientState);
WebControl ctrl = cpe.Parent.FindControl(cpe.TargetControlID) as WebControl;
if (ctrl!=null)
    ctrl.Style["height"] = collapsed ? "0px" : "";
Comment posted by daniel on Monday, July 19, 2010 12:33 PM
i create a new webapplication and copy the code to the page and work!

But when i copy the code to a webapplication that i have been working i get an error in :

$find("CollapsiblePanelExtender1");

this returns null... the version of ajax in the booth is 3.5

can you help, thanks
Comment posted by RituRaj Pandey on Saturday, July 24, 2010 9:25 AM
Thanks
Comment posted by Dennis on Tuesday, August 03, 2010 1:04 AM
very good article saved a lot of time keep it up

Post your comment
Name:  
E-mail: (Will not be displayed)
Comment:
Insert Cancel

NEWSLETTER