Create new account I forgot my password    

Creating a Cool UI effect on ASP.NET Panels using jQuery
Rating: 21 user(s) have rated this article Average rating: 4.2
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.
If you are just getting started with jQuery, check this: Using jQuery with ASP.NET - A Beginner's Guide. An updated version of jQuery is now available over here.  If you have been using version 1.2.6, replace it with the updated version.
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
jQuery Ref
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();).
Panel Effect
On clicking the ‘+’ image button, the first panel is shown with a sliding effect.
Panel Effect
Clicking the ‘+’ button shows the second panel and so on.
Panel Effect
Similarly clicking on the ‘-‘ button hides the last visible panel
Panel Effect
I hope you liked the article and I thank you for viewing it. The source code for this article can be downloaded from here.









Page copy protected against web site content infringement by Copyscape


How would you rate this article?

User Feedback
Comment posted by Ben White on Saturday, February 07, 2009 12:43 PM
Neat.Thanks for sharing.
Comment posted by Jason on Tuesday, April 21, 2009 2:53 PM
Please post demos.  It seems very 'un-jquery' to not have a demo link right near the top of your article...
Comment posted by Valamas on Tuesday, April 28, 2009 10:36 PM
Demos would be nice.
Comment posted by sapan on Wednesday, April 29, 2009 3:46 AM
I want a code in which the content inside the panel is slide on click event of link button  which are situated outside the panel
Comment posted by Brad on Thursday, April 30, 2009 10:21 AM
$("#btnShow") will only work if the control is not inside another control or if you are not using master pages.

Use  $("#<%= btnShow.ClientID %>") to get the generated control ID on your webforms page.
Comment posted by Musez on Wednesday, May 06, 2009 8:14 AM
Is there a way to automate this. So if I click '+', all panels should show one by one with fade effect. similarly reverse.
Comment posted by Hi on Monday, December 14, 2009 3:24 AM
Am using this inside a master page and as well am using component art control , but when i add the JS file its not displaying contents inside a panel . When i exculde JS file it shows the contents but does not work on click of show and hide buttons

Here is my code and i have added the JS file in master page



<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<table width="100%" class="cpHeader""><tr ><td align="left" style="width:10%; height:25">
    <div >
        <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>  </td> <td  align="center">Filters</td></tr>
    <tr style=" background-color:Aqua; ">
    <td colspan="2">
   <asp:Panel ID="Panel1" runat="server" class='cpBody'>
   <table width="100%" cellpadding="0" cellspacing="0">
   <tr height="20" valign="top">
   <td width="20%"  >Supplier</td>
    <td width="20%" >Department</td>
   <td width="20%"  >Category</td>
     <td width="20%" >Season</td>
     <td width="20%" >Search String</td>
      </tr>
       <tr><td width="20%" align="center" >
           <asp:DropDownList ID="cmbSupplier" runat="server" Width="90%">
           </asp:DropDownList>
           </td>
   <td width="20%" align="center">
       <asp:DropDownList ID="cmbDepartment" runat="server" Width="90%">
       </asp:DropDownList>
           </td>
   <td width="20%" align="center">
       <asp:DropDownList ID="cmbCategory" runat="server" Width="90%">
       </asp:DropDownList>
           </td>
   <td width="20%" align="center">
       <asp:DropDownList ID="cmbSeason" runat="server" Width="90%">
       </asp:DropDownList>
           </td>
           <td width="20%" align="center">
       <asp:TextBox ID ="txtSearchString" runat="server"  ></asp:TextBox>
           </td>
      </tr>
         </table>
         <br />
      <table width="100%" cellpadding="0" cellspacing="0">
       <tr><td  align="left" width = "25%" ><asp:CheckBox  runat="server" ID = "CheckBox5" Text="Show sotck only." /> </td>
   <td align="left" width = "25%" ><asp:CheckBox runat="server" ID = "chkStylesWithSplitSeason" Text="Show style." /> </td>
   <td align="left" width = "25%" ><asp:CheckBox runat="server" ID = "CheckBox1" Text="Show split season " /> </td>
   <td align="left" width = "25%" ><asp:CheckBox runat="server" ID = "CheckBox2" Text="Show style." /> </td>
      </tr>
      
      </table>
    </asp:Panel>
   </td></tr>
   </table>
  
<script type="text/javascript" language="javascript">
     $(document).ready(function() {
         var pan = $(".cpBody").hide();
         var showNext = 0;
         $("#<%= btnShow.ClientID %>").click(function() {
             if (showNext < pan.length) {
                 $(pan[showNext++]).slideDown();
             }
         });

       $("#<%= btnHide.ClientID %>").click(function() {
             if (showNext > 0) {
                 $(pan[showNext - 1]).slideUp();
                 showNext--;
             }
         });
     });
    </script>  
</asp:Content>


Waitning for the feedback .




Comment posted by Anandarajeshwaran.J on Tuesday, June 15, 2010 3:43 AM
None of the asp.net articles in your site is having a demo page.(a page which can demonstrate the effect you are speaking about). But you have a ton of information and tips grouped in one site. thanks

Post your comment
Name:
E-mail: (Will not be displayed)
Comment:
Insert Cancel

NEWSLETTER