块引用 我正在使用Bootstrap TabPane,我有两个Tabs,但是如果有任何'Auto post back'事件Fires我会松开当前的tab索引并转到第一个选项卡,默认情况下是活动的。 我尝试了在codeproject上找到的Javascript,但它的工作正常,因为按钮点击它们作为演示发送,当我将下拉列表放入其代码时它停止工作, 隐藏字段值为Comes null if if(this.ispostback){TabName.Value = Request.Form [TabName.UniqueID];}
我的第二个标签只对管理员可见,我必须隐藏它的服务器端。我想学习服务器端和客户端
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CS.aspx.cs" Inherits="CS" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.nav-tabs a, .nav-tabs a:hover, .nav-tabs a:focus
{
outline: 0;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript" src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>
<div class="panel panel-default" style="width: 500px; padding: 10px; margin: 10px">
<div id="Tabs" role="tabpanel">
<!-- Nav tabs -->
<ul class="nav nav-tabs" role="tablist">
<li><a href="#personal" aria-controls="personal" role="tab" data-toggle="tab">Personal
</a></li>
<li><a href="#employment" aria-controls="employment" role="tab" data-toggle="tab">Employment</a></li>
</ul>
<!-- Tab panes -->
<div class="tab-content" style="padding-top: 20px">
<div role="tabpanel" class="tab-pane active" id="personal">
This is Personal Information Tab
<asp:DropDownList ID="DropDownList1" CssClass="form-control" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" AutoPostBack="true" runat="server"></asp:DropDownList>
</div>
<div role="tabpanel" class="tab-pane" id="employment">
This is Employment Information Tab
</div>
</div>
</div>
<br />
<br />
<asp:Button ID="Button1" Text="Submit" runat="server" CssClass="btn btn-primary" />
<asp:HiddenField ID="TabName" runat="server" />
</div>
<script type="text/javascript">
$(function () {
var tabName = $("[id*=TabName]").val() != "" ? $("[id*=TabName]").val() : "personal";
$('#Tabs a[href="#' + tabName + '"]').tab('show');
$("#Tabs a").click(function () {
$("[id*=TabName]").val($(this).attr("href").replace("#", ""));
});
});
</script>
</form>
</body>
</html>
这是我的代码
public partial class CS : System.Web.UI.Page
{
MySqlConnection con = new MySqlConnection(ConfigurationManager.ConnectionStrings["Conn"].ConnectionString);
MySqlConnection con2 = new MySqlConnection(ConfigurationManager.ConnectionStrings["Con"].ConnectionString);
protected void Page_Load(object sender, EventArgs e)
{
if (this.IsPostBack)
{
TabName.Value = Request.Form[TabName.UniqueID];
}
if(!IsPostBack)
{
CrmNo();
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
}
void FillDDl()
{
//This code will Fill my Dropdown on page load
}
}
答案 0 :(得分:1)
我会使用jquery通过哈希调用选项卡。这样,正确的选项卡将保持选中状态。
如果您在html中的头标记内调用js,则可以执行此操作。
$(document).ready(function () {
if (location.hash != '') {
$('a[href="' + location.hash + '"]').tab('show');
}
else {
$('a[role="tab"]:first').tab('show');
}
$('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
if (history.pushState) {
history.pushState(null, null, '#' + $(e.target).attr('href').substr(1));
} else {
location.hash = '#' + $(e.target).attr('href').substr(1);
}
});
});
如果您在文档正文中调用js,那么您可以这样做。
if (location.hash != '') {
$('a[href="' + location.hash + '"]').tab('show');
}
else
{
$('a[role="tab"]:first').tab('show');
}
$('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
if (history.pushState) {
history.pushState(null, null, '#' + $(e.target).attr('href').substr(1));
} else {
location.hash = '#' + $(e.target).attr('href').substr(1);
}
});
这个js代码应该做的是调整URL,使其具有tab的id具有url的哈希值。这将确保在回发期间选项卡保持选中状态。
您当然需要确保此代码低于您对jquery js和bootstrap js文件的调用,以使其正常工作。