[英]How to select a jQuery tab from C# code behind?
I am using jQuery tabbed content.我正在使用 jQuery 选项卡式内容。
<div class="container">
<ul class="tabs">
<li>
<a id= "tab1" runat="server" href="#tab1" class="active" >Tab1</a>
</li>
<li>
<a id= "tab2" runat="server" href="#tab2" >Tab2</a>
</li>
</ul>
<div id="div1" class="form-action show">
........
</div>
<div id="div2" class="form-action hide">
.......
</div>
</div>
jQuery function jQuery 函数
(function ($) {
// constants
var SHOW_CLASS = 'show',
HIDE_CLASS = 'hide',
ACTIVE_CLASS = 'active';
$('.tabs').on('click', 'li a', function (e) {
e.preventDefault();
var $tab = $(this),
href = $tab.attr('href');
$('.active').removeClass(ACTIVE_CLASS);
$tab.addClass(ACTIVE_CLASS);
$('.show')
.removeClass(SHOW_CLASS)
.addClass(HIDE_CLASS)
.hide();
$(href)
.removeClass(HIDE_CLASS)
.addClass(SHOW_CLASS)
.hide()
.fadeIn(550);
});
})(jQuery);
Tabs are working fine.标签工作正常。 When a page is requested from another page, I want the selected tab according to the query string value.当从另一个页面请求一个页面时,我希望根据查询字符串值选择选项卡。 For example if I pass例如,如果我通过
<a href="Page.aspx?tab=tab1">Tab1</a>
then Tab1 should be selected.然后应选择 Tab1。 If I pass如果我通过
<a href="Page.aspx?tab=tab2">Tab2</a>
then Tab2 should be selected.然后应选择 Tab2。
If you are using jQuery UI, just use hashtag
and point to the tab ID;如果您使用的是 jQuery UI,只需使用hashtag
并指向选项卡 ID; jQuery will do the rest: jQuery 将完成剩下的工作:
<a href="Page.aspx#tab2">Tab2</a>
If you are not using jQuery UI, follow these instructions:如果您不使用 jQuery UI,请按照以下说明操作:
Use hashchange
event is better to defined which tab is clicked.使用hashchange
事件更好地定义单击哪个选项卡。 I have modified your code a bit, check it out.我稍微修改了你的代码,检查一下。
(function ($) {
// constants
var SHOW_CLASS = 'show',
HIDE_CLASS = 'hide',
ACTIVE_CLASS = 'active';
$(window).on('hashchange', function() {
href = window.location.hash;
if (href == "") return;
$('.tabs li a').removeClass('active');
$('.tabs li a[href=' + href + ']').addClass('active');
$('.show')
.removeClass(SHOW_CLASS)
.addClass(HIDE_CLASS)
.hide();
$(href)
.removeClass(HIDE_CLASS)
.addClass(SHOW_CLASS)
.hide()
.fadeIn(550);
});
$(window).trigger('hashchange'); // If the page is loaded from another page
})(jQuery);
HTML: HTML:
<asp:HiddenField runat="server" ID="hdn" />
Code Behind:背后的代码:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
if (Request.QueryString["tab"] != null)
{
hdn.Value = Request.QueryString["tab"].ToString();
}
}
}
CSS: CSS:
<style type="text/css">
.active {
background:Green;
}
</style>
jQuery: jQuery:
<script type="text/javascript">
$(document).ready(function () {
// constants
var SHOW_CLASS = 'show',
HIDE_CLASS = 'hide',
ACTIVE_CLASS = 'active';
$('.tabs').tabs();
var class_1 = $('#<%= hdn.ClientID %>').val();
$('.tabs > li').find('a').removeClass('active');
$('.tabs > li').each(function (index) {
if ($(this).find('a').attr('id') == class_1) {
$(this).find('a').addClass('active');
}
});
$('.tabs').on('click', 'li a', function (e) {
e.preventDefault();
var $tab = $(this),
href = $tab.attr('href');
$('.active').removeClass(ACTIVE_CLASS);
$tab.addClass(ACTIVE_CLASS);
$('.show')
.removeClass(SHOW_CLASS)
.addClass(HIDE_CLASS)
.hide();
$(href)
.removeClass(HIDE_CLASS)
.addClass(SHOW_CLASS)
.hide()
.fadeIn(550);
});
});
</script>
There are two things you must do:你必须做两件事:
1) In your Page.aspx.cs 1) 在你的 Page.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
//Get the query string called tab
private string tab = Request.Querystring("tab");
//Check that query string is not null
if(tab!=null)
{
//Run JavaScript. NB: the parameter passed to this is based off our query string
ScriptManager.RegisterStartupScript(this, typeof(string), "Registering", String.Format("openTab('{0}');", tab), true);
}
}
2) In your JavaScript, just create a new method 2) 在您的 JavaScript 中,只需创建一个新方法
function openTab(tab) {
//Will fake a user "click" for the tab that aSP.NET told it to open
$(".tabs li a").each(function(){
var id = $(this).attr("href").replace("#", "");
if(id==tab) $(this).trigger("click");
});
}
I have not tested this sorry, just give it a try!抱歉,我没有测试过,请试一试!
The key here is the ScriptManager that ASP.NET offers.这里的关键是 ASP.NET 提供的 ScriptManager。 It lets you communicate with a client side script, so you can just use this to do what you need to它允许您与客户端脚本进行通信,因此您可以使用它来执行您需要的操作
EDIT : I updated the JavaScript function of openTab()
.编辑:我更新了openTab()
的 JavaScript 函数。 You can see an example fiddle working here with the 2nd tab opening on load您可以在此处看到一个示例小提琴,在加载时打开第二个选项卡
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.