|
Creating a Cool UI effect on ASP.NET Panels using jQuery
|
|
Rating: 21 user(s) have rated this article
Posted by: Suprotim Agarwal,
on 2/2/2009,
in category "jQuery and ASP.NET"
Views: this article has been read 37571 times
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.