基于解决方案 1 的完整答案(效果很好,看起来很棒)。
将此添加到您的 aspx。
<style>
.floatingHeader {
position: fixed;
top: 0;
visibility: hidden;
background-color:antiquewhite;
z-index: 1000;
}
</style>
<script>
function UpdateTableHeaders() {
$(".persist-area").each(function () {
var el = $(this),
offset = el.offset(),
scrollTop = $(window).scrollTop(),
floatingHeader = $(".floatingHeader", this)
if ((scrollTop > offset.top) && (scrollTop < offset.top + el.height())) {
floatingHeader.css({
"visibility": "visible"
});
} else {
floatingHeader.css({
"visibility": "hidden"
});
};
});
}
// DOM Ready
$(function() {
$(window)
.scroll(UpdateTableHeaders)
.trigger("scroll");
});
window.onload = function () {
$("table.tableWithFloatingHeader").each(function () {
$(this).wrap("<div class='divTableWithFloatingHeader' style='position:relative'></div>");
$("tr:first", this).before($("tr:first", this).clone());
clonedHeaderRow = $("tr:first", this);
clonedHeaderRow.addClass("floatingHeader");
var row_ths = $("tr:nth-child(2)", this).children("th");
var crow_ths = $("tr:nth-child(1)", this).children("th"); ;
for (var i = 0; i < row_ths.size(); ++i) {
crow_ths.eq(i).width(row_ths.eq(i).width() );
}
});
}
window.onresize = function () {
$("table.tableWithFloatingHeader").each(function () {
clonedHeaderRow = $("tr:first", this);
var row_ths = $("tr:nth-child(2)", this).children("th");
var crow_ths = $("tr:nth-child(1)", this).children("th");;
for (var i = 0; i < row_ths.size() ; ++i) {
crow_ths.eq(i).width(row_ths.eq(i).width());
}
});
}
</script>
现在在你的表上添加以下两个类persist-area tableWithFloatingHeader 和 HeaderStyle Class of presist-header
<asp:GridView EnableViewState="false" ID="grid_Results" runat="server" AutoGenerateColumns="False" Class="persist-area tableWithFloatingHeader">
它会起作用的。
如果您还想更改标题的颜色,您需要执行以下两个步骤。
在后面加上
并在 aspx.vb 后面的代码中添加网格的数据绑定事件
Protected Sub grid_Results_DataBound(sender As Object, e As EventArgs) Handles grid_Results.DataBound
If grid_Results.HeaderRow IsNot Nothing Then
grid_Results.HeaderRow.TableSection = TableRowSection.TableHeader
End If
End Sub