我想修复我的 gridview 标题位置,以便在垂直滚动列标题时必须看到。我已经尝试了多种解决方案,但没有一个对我有用。以下是在 asp.net 中修复 gridview 标头最流行的代码,请解决我的问题,为什么它不起作用
css代码
<style type="text/css">
.WrapperDiv
{
width:560px;height:290px;border: 1px solid black;
}
.WrapperDiv TH
{
/* Needed for IE */
position: relative;
}
.WrapperDiv TR
{
/* Needed for IE */
height:0px;
}
</style>
javascript代码
<script type="text/ecmascript">
var headerHeight = 8;
freezeGridViewHeaderForIEAndFF('GridView2', 'WrapperDiv');
function freezeGridViewHeaderForIEAndFF(gridViewId, wrapperDivCssClass) {
debugger;
var grid = document.getElementById(gridViewId);
if (grid) {
if (grid.style && grid.style.visibility)
grid.style.visibility = 'hidden';
if (grid.parentNode) {
//Find wrapper div output by GridView
var div = grid.parentNode;
if (div.tagName == "div1") {
div.className = wrapperDivCssClass;
div.style.overflow = "auto";
}
}
var grid = prepareFixedHeader(grid);
var tbody = grid.getElementsByTagName('TBODY')[0];
//Needed for Firefox
tbody.style.height = (div1.offsetHeight - headerHeight) + 'px';
tbody.style.overflowX = "hidden";
tbody.overflow = 'auto';
tbody.overflowX = 'hidden';
grid.style.visibility = 'visible';
}
}
function prepareFixedHeader(grid) {
//Find DOM TBODY element and and
var tags = grid.getElementsByTagName('TBODY');
if (tags != 'undefined') {
var tbody = tags[0];
var trs = tbody.getElementsByTagName('TR');
if (trs != 'undefined') {
headerHeight += trs[0].offsetHeight;
//Remove first TR tag from it
var headTR = tbody.removeChild(trs[0]);
//create a new element called THEAD
var head = document.createElement('THEAD');
head.appendChild(headTR);
//add to a THEAD element instead of TR so CSS styles
//can be applied properly in both IE and FireFox
grid.insertBefore(head, grid.firstChild);
}
}
return grid;
}
</script>
网格视图 asp 代码
<div id="div1"style="overflow-x:hidden;overflow-y:auto;height:400px">
<table>
<asp:GridView ID="GridView2" style="table-layout:fixed;" Width="1000px" AutoGenerateColumns ="False" AllowSorting="true" runat="server"
CellPadding="4" ForeColor="#333333"
GridLines="None">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField HeaderText="Assembly" HeaderStyle-Font-Size="Small" >
<ItemTemplate >
<asp:label ID="label23" Font-Size="small" ToolTip ="click to see assembly page" runat="server" Text='<%# bind("AsbCd") %>' >
</asp:label>
</ItemTemplate>
<HeaderStyle BorderStyle="Outset" />
<ItemStyle BorderStyle="Outset" HorizontalAlign ="center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="AssemblyName" HeaderStyle-Font-Size="small" >
<ItemTemplate>
<asp:label ID="label24" Font-Size="Small" ToolTip ="click to see assembly page" runat="server" Text='<%# bind("AsbNm") %>' >
</asp:label>
</ItemTemplate>
<HeaderStyle BorderStyle="Outset" />
<ItemStyle BorderStyle="Outset" HorizontalAlign="Left" />
</asp:TemplateField>
<asp:BoundField HeaderText="Source" ItemStyle-Font-Size="Small" HeaderStyle-Font-Size="Small" HeaderStyle-CssClass="fixedheadercell" >
<HeaderStyle BorderStyle="Outset" />
<ItemStyle BorderStyle="Outset" HorizontalAlign ="left"/>
</asp:BoundField>
<asp:BoundField HeaderText="Operators" DataField="operators" ItemStyle-Font-Size="Small" HeaderStyle-Font-Size="Small" HeaderStyle-CssClass="fixedheadercell" >
<HeaderStyle BorderStyle="Outset" />
<ItemStyle BorderStyle="Outset" HorizontalAlign ="left"/>
</asp:BoundField>
<asp:BoundField HeaderText="Hours" DataField="Days" ItemStyle-Font-Size="Small" HeaderStyle-Font-Size="Small" HeaderStyle-CssClass="fixedheadercell" >
<HeaderStyle BorderStyle="Outset" />
<ItemStyle BorderStyle="Outset" HorizontalAlign ="Right"/>
</asp:BoundField>
<asp:BoundField DataField="PlannedDt" HeaderText="Start Date" ItemStyle-Font-Size="Small" HeaderStyle-Font-Size="Small" HeaderStyle-CssClass="fixedheadercell"
DataFormatString="{0:dd-MMM-yyyy}" HtmlEncode="false" >
<HeaderStyle BorderStyle="Outset" />
<ItemStyle BorderStyle="Outset" HorizontalAlign ="Right" />
</asp:BoundField>
<asp:BoundField DataField="PlancedEnddt" HeaderText="End Date" ItemStyle-Font-Size="Small" HeaderStyle-Font-Size="Small" HeaderStyle-CssClass="fixedheadercell"
DataFormatString="{0:dd-MMM-yyyy}" HtmlEncode="false" >
<HeaderStyle BorderStyle="Outset" />
<ItemStyle BorderStyle="Outset" HorizontalAlign ="Right"/>
</asp:BoundField>
<asp:BoundField DataField="Operator" HeaderText="Operator" ItemStyle-Font-Size="Small" HeaderStyle-Font-Size="Small" HeaderStyle-CssClass="fixedheadercell" >
<HeaderStyle BorderStyle="Outset" />
<ItemStyle BorderStyle="Outset" HorizontalAlign ="left"/>
</asp:BoundField>
<asp:BoundField DataField="ActualDays" HeaderText="Hours" ItemStyle-Font-Size="Small" HeaderStyle-Font-Size="Small" HeaderStyle-CssClass="fixedheadercell" >
<HeaderStyle BorderStyle="Outset" />
<ItemStyle BorderStyle="Outset" HorizontalAlign ="Right" />
</asp:BoundField>
<asp:BoundField DataField="ActualStartDt" HeaderText="Start Date " ItemStyle-Font-Size="Small" HeaderStyle-Font-Size="Small" HeaderStyle-CssClass="fixedheadercell"
DataFormatString="{0:dd-MMM-yyyy}" HtmlEncode="false" >
<HeaderStyle BorderStyle="Outset" />
<ItemStyle BorderStyle="Outset" HorizontalAlign ="Right" />
</asp:BoundField>
<asp:BoundField DataField="ActualEndDt" HeaderText="End Date" ItemStyle-Font-Size="Small" HeaderStyle-Font-Size="Small" HeaderStyle-CssClass="fixedheadercell "
DataFormatString="{0:dd-MMM-yyyy}" HtmlEncode="false" >
<HeaderStyle BorderStyle="Outset" />
<ItemStyle BorderStyle="Outset" HorizontalAlign ="Right" />
</asp:BoundField>
<asp:BoundField DataField="IssuePending" HeaderText="Shortfall" ItemStyle-Font-Size="Small" HeaderStyle-Font-Size="Small" HeaderStyle-CssClass="fixedheadercell " >
<HeaderStyle BorderStyle="Outset" />
<ItemStyle BorderStyle="Outset" HorizontalAlign ="Right" />
</asp:BoundField>
<asp:BoundField DataField="rack" HeaderText="Rack" ItemStyle-Font-Size="Small" HeaderStyle-Font-size="Small" HeaderStyle-CssClass="fixedheadercell" >
<HeaderStyle BorderStyle="Outset" />
<ItemStyle BorderStyle="Outset" HorizontalAlign ="Right" />
</asp:BoundField>
<asp:BoundField DataField="Remark" HeaderText="Remark" ItemStyle-Font-Size="Small" HeaderStyle-Font-Size="Small" HeaderStyle-CssClass="fixedheadercell" >
<HeaderStyle BorderStyle="Outset" />
<ItemStyle BorderStyle="Outset" HorizontalAlign ="Right" />
</asp:BoundField>
</Columns>
</asp:GridView>
</table>
</div>