Click here to Skip to main content
15,896,111 members
Articles / Web Development / ASP.NET
Tip/Trick

jQuery tab in ASP.NET: Active tab problem after postback

Rate me:
Please Sign up or sign in to vote.
4.53/5 (9 votes)
2 Jul 2013CPOL 62.1K   2.1K   7   14
Using jQuery tabs is easy to use. But the problem with ASP.NET is that when the page gets post back the active tab is deactivated and by default the first one gets activated. This aricle is about to fix this problem, i.e., after postback the same tab will be active.

Sample Image - maximum width is 600 pixels

Introduction

This article solves the deactivation of the active tab of the jQuery tab plugin after postback in ASP.NET.

Using the code

Create a new website in Visual Studio. Add the page defaualt.aspx.

Add the following code to default.aspx:

XML
<head>
    <title></title>
    <script src="js/jquery-1.9.1.js" type="text/javascript"></script>
    <link href="css/custom-theme/jquery-ui-1.10.1.custom.min.css" rel="stylesheet" type="text/css" />
    <script src="js/jquery-ui-1.10.1.custom.js" type="text/javascript"></script>
    <script language="javascript" type="text/javascript">
        $(document).ready(function () {

            $('#tabs').tabs({
                activate: function () {
                    var newIdx = $('#tabs').tabs('option', 'active');
                    $('#<%=hidLastTab.ClientID%>').val(newIdx);

                }, heightStyle: "auto",
                active: previouslySelectedTab,
                show: { effect: "fadeIn", duration: 1000 }
            });

        });
 </script>

</head>
<body>
    <form id="form1" runat="server">
    <div style="width:900px; margin:0 auto">
    <div id="tabs" style="margin:0 auto;  margin-bottom:2px;">
	<ul>
		<li><a href="#tabs-1">STATE TRACKING</a></li>
		<li><a href="#tabs-2">ICONS</a></li>
		<li><a href="#tabs-3">Effects</a></li>
        
	</ul>
    <div id="tabs-1">
    <strong>STATE TRACKING</strong>
        <p>
At any point in time, various parts of widget or interaction elements may be in various
states. jQuery UI tracks these states, and applies appropriate visual styling to them, via
a set of classes that begin with ui-state. These states include ui-state-default, uistate-
active, ui-state-focus, ui-state-highlight, ui-state-error, ui-statedisabled,
and ui-state-hover.
We can use these names in our own scripts or CSS to track state or affect the styling
of elements in the various states.</p>
    </div>
     <div id="tabs-2">
     <strong>ICONS</strong><p>
jQuery UI defines a large number of icons that can be used by various widgets. For
example, icon indicators on the tab elements of the Tab widget, or icons directly on
Button widgets. Each icon is identified by a class name beginning with ui-icon; for
example, ui-icon-person, ui-icon-print, and ui-icon-arrowthick-1-sw.</p><p>
jQuery UI is very clever regarding how it handles icons. All the individual icon
images are defined in a grid on a single image—an icon sheet, if you will. That way,
once this image has been downloaded and cached by the browser, no further trips to
the server are needed to display any of the available icons—and there are a lot of
them (173 as this is being written). The icon class definitions merely identify how to
move the origin of this sheet image as a background image, causing the desired icon
to appear as the background of an element.</p>
    </div>
      <div id="tabs-3">
    <strong> The jQuery UI effects</strong><p>
All of the effects that jQuery UI provides can be used on their own—without other
methods—via the effect() method. This method launches the effect on the elements
of the wrapped set.</p>
    </div>
   
  </div>
  <asp:Button ID="btnSubmit" runat="server" Text="submit" />
    <asp:HiddenField ID="hidLastTab" Value="0" runat="server" />
    </div>
    
    </form>
</body>

/////////////////// code to be added in the default.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
    String hiddenFieldValue = hidLastTab.Value;
    StringBuilder js = new StringBuilder();
    js.Append("<script type='text/javascript'>");
    js.Append("var previouslySelectedTab = ");
    js.Append(hiddenFieldValue);
    js.Append(";</script>");
    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "acttab", js.ToString());
    //this.Header.Controls.Add(new LiteralControl(js.ToString()));
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Pakistan Pakistan
i am working in asp.net since 2007. My favorite is c#.
Email: zafar.kust@gmail.com
skype: zafarali5

Comments and Discussions

 
QuestionDoesn't work with UpdatePanel Pin
Payal Sathe14-Oct-15 21:17
Payal Sathe14-Oct-15 21:17 
QuestionSimple and easy Pin
Ronald.A24-Jun-15 4:31
Ronald.A24-Jun-15 4:31 
BugDoes not work Pin
Member 1145121113-Feb-15 15:52
Member 1145121113-Feb-15 15:52 
GeneralMy vote of 1 Pin
Member 1145121113-Feb-15 15:51
Member 1145121113-Feb-15 15:51 
Questionprevious tab style still selected after postback Pin
Member 1094484330-Dec-14 6:14
Member 1094484330-Dec-14 6:14 
QuestionNot getting the tabs Pin
Member 109491169-Oct-14 16:38
Member 109491169-Oct-14 16:38 
AnswerRe: Not getting the tabs Pin
Zafar A khan 10-Oct-14 18:53
professionalZafar A khan 10-Oct-14 18:53 
Questionhi sir faced this problem i tried your code and other code also but i stuck in the same problem Pin
Omkar Hendre19-Sep-14 1:49
Omkar Hendre19-Sep-14 1:49 
Actually I have three different tabs and i have three different buttons inside these three tabs when i click on a button of second tab then after updating my form i redirected to first tab but i want redirect the user to his previous selected tab
please help me to solve my problem and thank you very much for your help
Omkar Hendre
Jr. Software Developer

AnswerRe: hi sir faced this problem i tried your code and other code also but i stuck in the same problem Pin
Zafar A khan 19-Sep-14 4:22
professionalZafar A khan 19-Sep-14 4:22 
AnswerRe: hi sir faced this problem i tried your code and other code also but i stuck in the same problem Pin
Zafar A khan 10-Oct-14 18:55
professionalZafar A khan 10-Oct-14 18:55 
QuestionDoubt Pin
Member 110303599-Sep-14 10:21
Member 110303599-Sep-14 10:21 
GeneralPlus one Pin
Serenity117-Mar-14 23:27
Serenity117-Mar-14 23:27 
QuestionNice Pin
Serenity117-Mar-14 23:26
Serenity117-Mar-14 23:26 
QuestionjQuery tab in ASP.NET Pin
mab-designer3-Aug-13 2:49
professionalmab-designer3-Aug-13 2:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.