简体   繁体   English

Jquery回发,在回发后维护相同的选项卡

[英]Jquery postback, maintain same tab after postback

I'm using jquery tab and following js method, how and what can i modify it to maintain state of tab between postbacks? 我正在使用jquery选项卡并遵循js方法,如何以及如何修改它以维护回发之间的tab状态? (This resets tabs to first tab after page_load) (这会在page_load之后将制表符重置为第一个制表符)

$(document).ready(function() {

        //When page loads...
        $(".tab_content").hide(); //Hide all content
        $("ul.tabs li:first").addClass("active").show(); //Activate first tab
        $(".tab_content:first").show(); //Show first tab content

        //On Click Event
        $("ul.tabs li").click(function() {

            $("ul.tabs li").removeClass("active"); //Remove any "active" class
            $(this).addClass("active"); //Add "active" class to selected tab
            $(".tab_content").hide(); //Hide all tab content

            var activeTab = $(this).find("a").attr("href"); //Find the href attribute value to identify the active tab + content
            $(activeTab).fadeIn(); //Fade in the active ID content
            return false;
        }); 

You can track the active tab in a hidden field using Javascript, then check the hidden field when the page is loaded. 您可以使用Javascript跟踪隐藏字段中的活动选项卡,然后在加载页面时检查隐藏字段。 (Also in Javascript) (也是在Javascript中)

Alternatively, you can use UpdatePanels with ASP.Net AJAX to eliminate the postbacks. 或者,您可以将UpdatePanels与ASP.Net AJAX一起使用来消除回发。 (Note that if the tabs are in an update panel, they won't work correctly) (请注意,如果选项卡位于更新面板中,它们将无法正常工作)

An alternative to using a hidden field is to use the cookie property on the tab control 使用隐藏字段的替代方法是使用选项卡控件上的cookie属性

$("#tabs").tabs({ cookie: { expires: 1 } }); $(“#tabs”)。tabs({cookie:{expires:1}});

You need to reference the jquery.cookie.js file for this to work 您需要引用jquery.cookie.js文件才能生效

jQuery tabs cookie jQuery选项卡cookie

Try the following: 请尝试以下方法:

<p class="hiddenData"><asp:HiddenField ID="hdnData" runat="server" /></p>

<script type="text/javascript">
    $(document).ready(function() {
        $('.tabs li a').click(function() { });
        $('.tabs li').hover(function() {
            var liData = $(this);
            $('.hiddenData input:hidden').val(liData.find('span').text());
        });
        if ($('.hiddenData input:hidden').val() != '') {
            var liList = $('.tabs li');
            var hiddenData = $('.hiddenData input:hidden').val();
            liList.each(function() {
                if ($(this).find('span').text() == hiddenData) {
                    $(this).find('a').click();
                }
            });
        }
    });
</script>

You said 你说

 //When page loads...
 $(".tab_content").hide(); //Hide all content

I would load the this with css since that's faster. 我会用css加载它,因为它更快。 hide is probably doing a display:none; hide可能正在显示:none;

one solution is writing javascript from codebehind. 一个解决方案是从codebehind编写javascript。
and example with c# 和c#的例子

var selectedTab = IsAdvancedSearch ? "{'selected':1}" : String.Empty;
ScriptManager.RegisterClientScriptBlock(this, GetType(), "search", String.Format(@"jQuery(document).ready(function() {{ jQuery('#search').tabs({0}); }});", selectedTab), true);

or you could add an attribute to the #search tag with C# and read it with js 或者您可以使用C#向#search标记添加属性并使用js读取它

C# C#

search.Attributes.Add("selectedtab", "1");

JS JS

jQuery("#search").attr("selectedtab");

and another solution is with querystring. 另一个解决方案是使用查询字符串。
you can read the value from the querystring. 你可以从查询字符串中读取值。

I would personally choose the first or the second one. 我个人会选择第一个或第二个。

The hidden field approach works well for me. 隐藏的现场方法对我很有用。 With the .aspx containing 包含.aspx

<asp:HiddenField runat="server" ID="hfLastTab" Value="0" />

and the js ready function containing 和包含的js ready函数

$("#tabs").tabs({ active: <%= hfLastTab.Value %> });

the active tab will be set per the hidden field. 将根据隐藏字段设置活动选项卡。 (That's the jQuery UI v1.9 property, 'active' fka 'selected'.) The various controls that postback can provide for setting hfLastTab.Value to the appropriate index. (这是jQuery UI v1.9属性,'active'fka'selected'。)回发的各种控件可以提供将hfLastTab.Value设置为适当的索引。

I also wanted to be able to point to a particular tab with a URL from another page, and spent a lot of time thrashing adding and trying to parse a hash ref off the end, before going to a querystring parameter, ?t=N. 我还希望能够指向具有来自另一个页面的URL的特定选项卡,并且在转到查询字符串参数之前花费了大量时间来添加并尝试解析散列引用,?t = N. I parse that out in a !Page.IsPostback branch of Page_Load(). 我在Page_Load()的.Page.IsPostback分支中解析了它。 For fresh page loads, we go to tab 0 if nothing's spec'd, or tab N if the querystring has the parameter. 对于新页面加载,如果没有指定,我们转到选项卡0,如果查询字符串具有参数,则转到选项卡N. Lots of ways to handle that parsing. 有很多方法可以处理解析。 Here's one: 这是一个:

        if (!Page.IsPostBack) {
            string pat = @"t=(\d)";
            Regex r = new Regex(pat, RegexOptions.IgnoreCase);
            Match m = r.Match(Request.Url.Query);
            if (m.Success) hfLastTab.Value = m.Groups[0].ToString();
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM