Creating CollapsiblePanelExtender Functionality using ASP.NET and jQuery
Posted by: Suprotim Agarwal ,
on 12/6/2008,
in
Category jQuery and ASP.NET
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
Add two panel controls (<asp:panel >) to the page. Rename them to ‘pHeader’ and ‘pBody’ respectively.
<body>
<form id="form1" runat="server">
<div>
<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>
</div>
</form>
</body>
</html>
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">
.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>
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() {
$('#pBody').slideToggle('slow');
});
});
</script>
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:
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.
This article has been editorially reviewed by Suprotim Agarwal.
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!
Was this article worth reading? Share it with fellow developers too. Thanks!
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 received the prestigious Microsoft MVP award for 17 consecutive years, until he resigned from the program in 2025. 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