Creating a Cool UI effect on ASP.NET Panels using jQuery
Posted by: Suprotim Agarwal ,
on 2/2/2009,
in
Category jQuery and ASP.NET
Abstract: In one my previous articles, I had discussed how to Create CollapsiblePanelExtender Functionality using ASP.NET and jQuery. In this article, we will see how to create another Cool UI effect on the ASP.NET Panels using jQuery.
Creating a Cool UI effect on ASP.NET Panels using jQuery
In this article, we will see how to create a Cool UI effect on the ASP.NET Panels using jQuery. In one my previous articles, I had discussed how to Create CollapsiblePanelExtender Functionality using ASP.NET and jQuery. In this article, we will see how to create a Sliding effect on a bunch of ASP.NET Panels. The user will have control over showing and hiding panels, on the click of buttons. To start off with, only a ‘+’ and a ‘–‘ image button will be visible in the header panel. A body panel will be made visible, one at a time, whenever the user will click on the ‘+’ button. Similarly the panels will be hidden on each click of the ‘–‘ image button. Let us see how to achieve this effect.
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.3.1.min.js) > Select the file and click Add.
Drag and drop the jquery-1.3.1.min.js file from the Solution Explorer on to your page <head> to create a reference as shown below
Now add a Header and a few Body panels to the page. I have also added an Images folder which contains images for Show and Hide as shown below:
<body>
<form id="form1" runat="server">
<div>
<div class="cpHeader">
<asp:ImageButton ID="btnShow" ImageUrl="~/Images/Show.gif" runat="server" OnClientClick="return false;" />
<asp:ImageButton ID="btnHide" ImageUrl="~/Images/Hide.gif" runat="server" OnClientClick="return false;" />
</div>
<asp:Panel ID="Panel1" runat="server" class='cpBody'>
<asp:Label ID="Label1" runat="server" Text="Label">Panel 1 Content</asp:Label>
</asp:Panel>
<asp:Panel ID="Panel2" runat="server" class='cpBody'>
<asp:Label ID="Label2" runat="server" Text="Label">Panel 2 Content</asp:Label>
</asp:Panel>
<asp:Panel ID="Panel3" runat="server" class='cpBody'>
<asp:Label ID="Label3" runat="server" Text="Label">Panel 3 Content</asp:Label>
</asp:Panel>
</div>
</form>
</body>
Observe that we have set the CssClass on the header and body panels to add some styling to the controls. The CSS will look similar to the following:
<head runat="server">
<title>UI Effect with ASP.NET Panel</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>
The final step is to add some UI effects to the Panel.
Add the following jQuery:
...
<script type="text/javascript">
$(document).ready(function() {
var pan = $(".cpBody").hide();
var showNext = 0;
$("#btnShow").click(function() {
if (showNext < pan.length) {
$(pan[showNext++]).slideDown();
}
});
$("#btnHide").click(function() {
if (showNext > 0) {
$(pan[showNext - 1]).slideUp();
showNext--;
}
});
});
</script>
</head>
When you run the application, all the panels are hidden($(".cpBody").hide();).
On clicking the ‘+’ image button, the first panel is shown with a sliding effect.
Clicking the ‘+’ button shows the second panel and so on.
Similarly clicking on the ‘-‘ button hides the last visible panel
I hope you liked the article and I thank you for viewing it. The source code for this article can be downloaded from here.
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 has received the prestigious Microsoft MVP award for Sixteen consecutive years. 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