Saturday, March 23, 2013

Create a Tab control using JQuery in ASP.NET

By using the below code we can make a simple JQuery tab control. In this example i am going to create two tab. When we click a tab a table will be hide and another table will be visible.
  • Befor start coding download and add latest JQuery library and refer it in code like below inside head section.
  1. <script src='js/common/jquery-1.9.1.js' ></script>
  •  Then add the below JQuery code inside head section in ASP.NET HTML design page

  1. <script type="text/javascript">
  2.     $(document).ready(function () {
  3.         $(".tabLink").each(function () {
  4.             $(this).click(function () {
  5.                 tabeId = $(this).attr('id');
  6.                 $(".tabLink").removeClass("activeLink");
  7.                 $(this).addClass("activeLink");
  8.                 $(".tabcontent").addClass("hide");
  9.                 $("#" + tabeId + "-1").removeClass("hide")
  10.                 return false;
  11.             });
  12.         });
  13.     });
  14.  </script>
  • Then add the below code in body section. This is actually a tab design.
  1. <table cellpadding="0" cellspacing="0">
  2. <tr>
  3. <td class="contact_tab_box">
  4. <a href="javascript:;" class="tabLink activeLink" id="tab1">Tab1</a>
  5. <a href="javascript:;" class="tabLink " id="cont-2">Tab2</a>
  6. </td>
  7. </tr>
  8. </table>
  • Now we have to design two tables to work with the tab when we click it
  1. <table width="100%" class="tabcontent" id="t1">
  2. <tr>
  3. <td>
  4. DO YOUR DESIGN HERE
  5. </td>
  6. </tr>
  7. </table>

  8. <table width="100%" class="tabcontent hide" id="t2">
  9. <tr>
  10. <td>
  11. DO YOUR DESIGN HERE
  12. </td>
  13. </tr>
  14. </table>
  • We need the below CSS for better design
  1. .contact_tab_bar_left {
  2.     background: #F2F2F2;
  3. }
  4. .contact_tab_bar_right {
  5.     background: #F2F2F2;
  6.     border-radius:0px 6px 6px 0px;
  7. }
  8. .contact_tab_box {
  9.     background: #EEEEEE;
  10. }
  11. .contact_tab_box a {
  12.     font-family:Arial;
  13.     font-size:14px;
  14.     border:1px solid #797979;
  15.     color:#000;
  16.     padding: 7px 30px;
  17.     text-decoration:none;
  18.     background-color: #eee;
  19.     border-radius: 6px 6px 0px 0px;
  20.     -moz-border-radius: 6px 6px 0px 0px;
  21.     -webkit-border-radius: 6px 6px 0px 0px;
  22. }
  23. .contact_tab_box a.activeLink {
  24.     font-family:Arial;
  25.     font-size:14px;
  26.     font-weight: bold;
  27.     background-color: #3F3F3F;
  28.     color:#FFFFFF;
  29.     border-bottom: 0;
  30.     padding: 7px 30px;
  31.     border-radius: 6px 6px 0px 0px;
  32.     -moz-border-radius: 6px 6px 0px 0px;
  33.     -webkit-border-radius: 6px 6px 0px 0px;
  34. }
  35. .contact_tab_content {
  36.     border: 1px solid #797979;
  37.     -moz-border-radius: 6px;
  38.     -webkit-border-radius: 6px;
  39.     border-radius: 6px;
  40. }
  41. .hide { display: none;}

No comments:

Post a Comment