|
Creating CollapsiblePanelExtender Functionality using ASP.NET and jQuery
|
|
Rating: 22 user(s) have rated this article
Posted by: Suprotim Agarwal,
on 12/6/2008,
in category "jQuery and ASP.NET"
Views: this article has been read 36026 times
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.