Creating CollapsiblePanelExtender Functionality using ASP.NET and jQuery

Posted by: Suprotim Agarwal , on 12/6/2008, in Category jQuery and ASP.NET
Views: 90180
Abstract: In this article, we will see how to use ASP.NET and jQuery to build functionality similar to what the ASP.NET AJAX CollapsiblePanelExtender provides.
Creating CollapsiblePanelExtender Functionality using ASP.NET and jQuery
I had recently written an article ASP.NET AJAX CollapsiblePanelExtender - Tips and Tricks which showed how to use the ASP.NET AJAX CollapsiblePanelExtender control to easily add collapsible sections to your web page. In this article, I will show you how to create a similar functionality using jQuery with ASP.NET. Ever since I have been introduced to jQuery by one of my colleagues Govind Kanshi (Microsoft), I have been experimenting with jQuery and plan to share my work with my readers. This article is one such experiment that I did with ASP.NET and jQuery.If you are just getting started with jQuery, check this: Using jQuery with ASP.NET - A Beginner's Guide. Let us get started.
Open Visual Studio 2008 > File > New > Website > Choose ‘ASP.NET 3.5 website’ from the templates > Choose your language (C# or VB) > Enter the location > Ok. In the Solution Explorer, right click your project > New Folder > rename the folder as ‘Scripts’.
Right click the Scripts folder > Add Existing Item > Browse to the path where you downloaded the jQuery library (jquery-1.2.6.js) and the intellisense documentation (jquery-1.2.6-vsdoc.js) > Select the files and click Add.
Now drag and drop the jquery-1.2.6.js file from the Solution Explorer on to your page to create a reference as shown below
jQuery Ref
Add two panel controls (<asp:panel >) to the page. Rename them to ‘pHeader’ and ‘pBody’ respectively.
    <form id="form1" runat="server">
            <asp:Panel ID="pHeader" runat="server" CssClass="cpHeader">
                <asp:Label ID="lblText" runat="server" />
            <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
Observe that we have set the CssClass on the two panels to add some styling to the controls. The CSS will look similar to the following:
<head runat="server">
    <title>Collapsible Panel Extender using jQuery</title>
    <script src="Scripts/jquery-1.2.6.js" type="text/javascript"></script> 
     <style type="text/css">
            color: white;
            background-color: #719DDB;
            font: bold 11px auto "Trebuchet MS", Verdana;
            font-size: 12px;
            cursor: pointer;
            padding: 4px;           
            background-color: #DCE4F9;
            font: normal 11px auto Verdana, Arial;
            border: 1px gray;               
            padding: 4px;
            padding-top: 7px;
The final step is to add collapsible behavior to the panels, similar to what the ASP.NET CollapsiblePanelExtender provides.
Add the following jQuery:
<head runat="server">
    <title>Collapsible Panel Extender using jQuery</title>
    <script src="Scripts/jquery-1.2.6.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(document).ready(function() {
            $('#pHeader').click(function() {
When the user clicks the header panel (pHeader), we run a function which toggles the visibility of ‘pBody’ by adjusting its height in a ‘sliding’ manner. You can choose one of the three predefined speeds: slow, normal or fast. The speed can also be defined by specifying the number of milliseconds.
Run the application and click on the header panel to toggle the visibility of the panel body as shown below:
On page load:
When the user clicks on the header panel to collapse the body(pBody):
pBody in a collapsed state
User clicks on the header panel to expand the body:
Load again
And that’s all is required to create a functionality similar to the CollapsiblePanelExtender. Simple yet powerful! And that’s what jQuery is all about. I hope you liked the article and I thank you for reading it. 
 If you liked the article,  Subscribe to my RSS Feed or Subscribe Via Email

This article has been editorially reviewed by Suprotim Agarwal.

Absolutely Awesome Book on C# and .NET

C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.

We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).

Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.

Click here to Explore the Table of Contents or Download Sample Chapters!

What Others Are Reading!
Was this article worth reading? Share it with fellow developers too. Thanks!
Share on LinkedIn
Share on Google+

Suprotim Agarwal, MCSD, MCAD, MCDBA, MCSE, is the founder of DotNetCurry, DNC Magazine for Developers, SQLServerCurry and DevCurry. He has also authored a couple of books 51 Recipes using jQuery with ASP.NET Controls and The Absolutely Awesome jQuery CookBook.

Suprotim has received the prestigious Microsoft MVP award for ten consecutive times. In a professional capacity, he is the CEO of A2Z Knowledge Visuals Pvt Ltd, a digital group that offers Digital Marketing and Branding services to businesses, both in a start-up and enterprise environment.

Get in touch with him on Twitter @suprotimagarwal or at LinkedIn

Page copy protected against web site content infringement 	by Copyscape

Feedback - Leave us some adulation, criticism and everything in between!
Comment posted by Soeren on Thursday, January 22, 2009 6:14 AM
Cool example. I just tried it in a project I'm working on, and it works great. How ever, I downloaded the newest jQuery version 1.3.1, and the slideToggle seems to have a different behavior. It collapses from top to bottom, like an accordion. As soon as I referenced version 1.2.6 again, it worked as it should...
Comment posted by Soeren on Thursday, January 22, 2009 6:17 AM
What I meant was, in 1.3.1, it collapses from top and bottom into the middle, creating an accordion style collapse...
Comment posted by Soeren on Thursday, January 22, 2009 6:36 AM
What I meant was, in 1.3.1, it collapses from top and bottom into the middle, creating an accordion style collapse...
Comment posted by surendra on Thursday, January 22, 2009 8:41 AM
Comment posted by BJ on Thursday, January 22, 2009 11:29 AM
nice example, thanks for the example. in the same whay do you have any idea how to implement supressPostBack though JQuery.
Comment posted by Suprotim Agarwal on Thursday, January 22, 2009 11:50 AM
Soeren: Thanks for your comments. I will try it out at my end to study the behaviour

BJ: In order to prevent a postback, you can use this simple line of code. I assume a postback occurs in your form on a button click

Button1.Attributes.Add("onclick", "CallToJSFunction(); return false;")
Comment posted by BJ on Thursday, January 22, 2009 12:30 PM
Suprotim, thanks for your replay, but let me give you brif idea. I am Using Nested Listview control (3 Level)for collaps/expand feature. but i m not using colepsible pannel but using javascript. now once i change the dropdownlist it should maintain the client state. if i use collepsible pannel with supresspostback = true then it can be done. but i m strugling to do this way so i thought i can just implement supresspostback event throgh jScript then i can maintain client state after postback.
Comment posted by Andrew on Tuesday, February 3, 2009 1:26 AM
Comment posted by sandy on Thursday, March 12, 2009 10:53 AM
Hey where can i downlaod the jquery file?please tell me
Comment posted by Suprotim Agarwal on Sunday, March 15, 2009 1:41 AM
Sandy: I have posted a link in the first para of the article that explains it all
Comment posted by kamrul on Saturday, May 23, 2009 10:52 AM
Pls help????when i set vertical scroll true in pBody and insert a gridview within this panel, then for the first times it shows the scroll but when i click for collapse in and out then it removes the scroll, what is the solution???
Comment posted by Kartik Tankaria on Saturday, January 22, 2011 6:50 AM
Ur article was like a god to me thanks a ton!!! god bless u n write more articles like dis
Comment posted by Suprotim Agarwal on Monday, January 24, 2011 2:59 AM
Kartik: You are welcome :)
Comment posted by Gabriel on Thursday, February 17, 2011 11:21 AM
Sorry but with the jquery 1,5 version did not work for me, any idea??
Comment posted by nelson on Tuesday, March 29, 2011 10:41 PM
Does this example also work in an application using .NET 2.0 framework with visual studio 2005? since I can't make work on my end following the example.
Comment posted by Razalan on Tuesday, May 14, 2013 3:38 AM
Very good and helpful article, but how do I put in collapse state at page load.
Comment posted by Razalan on Tuesday, May 14, 2013 5:15 AM
thanks again. you really provide a good starting point for me as a beginner in jquery. I just found how to collapse at start.