冻结动态网格的标题行

Freezing Header Row of a dynamic Grid

本文关键字:标题 网格 动态 冻结      更新时间:2023-09-26

我有一个正在动态创建的网格视图。我在运行时将它添加到我的表单中(所以我的HTML源代码没有标记)。我已经设法获得了滚动条,但我的经理要求我在滚动时冻结标题行。在互联网上进行的详尽搜索只显示了静态网格视图的示例。我试图实现一个通用的javascript解决方案,但出现了一个错误"Object required",标记为:

var gridWidth = grid.offsetWidth;  

这是我的html来源:

form id="form1" runat="server">
<div>
    <asp:ImageButton ID="ImageButtonExcelExport" runat="server"
        ImageUrl="~/css/images/icons/custom/Excel-16.gif" 
        ToolTip="Export to Excel" PostBackUrl="~/ErrorReportGrid.aspx" />

    <div id="Div1" runat="server" >
    </div>

</div>
<!-- javascript here -->
<script type = "text/javascript">
var GridId = "<%=ErrorCodeGrid.ClientID %>";
var ScrollHeight = 300;
window.onload = function() {
    var grid = document.getElementById(GridId);
    var gridWidth = grid.offsetWidth;
    var gridHeight = grid.offsetHeight;
    var headerCellWidths = new Array();
for (var i = 0; i < grid.getElementsByTagName("TH").length; i++) {
        headerCellWidths[i] = grid.getElementsByTagName("TH")[i].offsetWidth;
    }
grid.parentNode.appendChild(document.createElement("div"));
    var parentDiv = grid.parentNode;
    var table = document.createElement("table");
for (i = 0; i < grid.attributes.length; i++) {
        if (grid.attributes[i].specified && grid.attributes[i].name != "id") {
            table.setAttribute(grid.attributes[i].name, grid.attributes[i].value);
        }
    }
    table.style.cssText = grid.style.cssText;
    table.style.width = gridWidth + "px";
    table.appendChild(document.createElement("tbody"));
    table.getElementsByTagName("tbody")[0].appendChild(grid.getElementsByTagName("TR") [0]);
    var cells = table.getElementsByTagName("TH");
    var gridRow = grid.getElementsByTagName("TR")[0];
for (i = 0; i < cells.length; i++) {
        var width;
        if (headerCellWidths[i] > gridRow.getElementsByTagName("TD")[i].offsetWidth) {
            width = headerCellWidths[i];
        }
        else {
            width = gridRow.getElementsByTagName("TD")[i].offsetWidth;
        }
        cells[i].style.width = parseInt(width - 3) + "px";
        gridRow.getElementsByTagName("TD")[i].style.width = parseInt(width - 3) + "px";
    }
parentDiv.removeChild(grid);
    var dummyHeader = document.createElement("div");
    dummyHeader.appendChild(table);
    parentDiv.appendChild(dummyHeader);
    var scrollableDiv = document.createElement("div");
if (parseInt(gridHeight) > ScrollHeight) {
        gridWidth = parseInt(gridWidth) + 17;
    }
scrollableDiv.style.cssText = "overflow:auto;height:" + ScrollHeight + "px;width:" + gridWidth + "px";
    scrollableDiv.appendChild(grid);
    parentDiv.appendChild(scrollableDiv);
}</script>

这是我的vb代码:

Public ErrorCodeGrid As GridView
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    'Create dynamic grid
    ErrorCodeGrid = New GridView
    ErrorCodeGrid.ID = "ErrorCodeGrid"
    'form1.Controls.Add(ErrorCodeGrid)
    Div1.Attributes("Style") = "width: 930px; height: 280px; margin-top: 0px; overflow: auto"
    Div1.Controls.Add(ErrorCodeGrid)
    'Set datasource ID for dynamic grid / sets the data that is displayed
    If Session("strVariableA") = "SINGLEUSER" And Session("strVariableB") = "EMPTY" Then 'staff sees their personal error
        ErrorCodeGrid.DataSource = SqlDataSourceSingleUser
        strSQL = SqlDataSourceSingleUser.SelectCommand.Replace("@User_ID", "'" + Session("strVariableB") + "'")
    ElseIf Session("strVariableA") = "ALL_RECORDS" Then 'user selects the list item "All Records"
        If Session("strVariableB") = "READ" Then 'if user has READ access, sees everyone's errors
            ErrorCodeGrid.DataSource = SqlDataSourceErrorReports_Core_Data
            strSQL = SqlDataSourceErrorReports_Core_Data.SelectCommand.ToString
        Else 'user sees all errors but Provider Data's
            ErrorCodeGrid.DataSource = SqlDataSourceStaffNoUserID
            strSQL = SqlDataSourceStaffNoUserID.SelectCommand.ToString
        End If
    ElseIf Session("strVariableA") = "CATEGORY_DESCRIPTIONS" Then 'user selects the list item "Category Descriptions"
        ErrorCodeGrid.DataSource = SqlDataSourceErrorDescriptions
        strSQL = SqlDataSourceErrorDescriptions.SelectCommand.ToString
    ElseIf Session("strVariableA") = "ACTIVE_ERROR" Then 'user selects a list item within the Code dropdown
        ErrorCodeGrid.DataSource = SqlDataSourceActiveErrors
        strSQL = SqlDataSourceActiveErrors.SelectCommand.Replace("@Error_Code", "'" + Session("strVariableB") + "'")
    ElseIf Session("strVariableA") = "STAFF_MEMBER" Then 'manager sees the selected user's errors
        ErrorCodeGrid.DataSource = SqlDataSourceMgrSingleUser
        strSQL = SqlDataSourceMgrSingleUser.SelectCommand.Replace("@User_ID", "'" + Session("strVariableB") + "'")
    ElseIf Session("strVariableA") = "TEAM" Then 'manager sees the selected team's errors
        ErrorCodeGrid.DataSource = SqlDataSourceMgrTeam
        strSQL = SqlDataSourceMgrTeam.SelectCommand.Replace("@Team_Name", "'" + Session("strVariableB") + "'")
    End If
    ErrorCodeGrid.DataBind()
    'set gridview parameters
    ErrorCodeGrid.EmptyDataText = "You currently have no errors."
    ErrorCodeGrid.CellPadding = 2
    ErrorCodeGrid.AllowSorting = True
    ErrorCodeGrid.CellSpacing = 1
    ErrorCodeGrid.Height = Unit.Pixel(111)
    ErrorCodeGrid.Width = Unit.Pixel(2775)
    ErrorCodeGrid.RowStyle.Font.Size = 8
    'set gridview colors
    ErrorCodeGrid.ForeColor = Drawing.ColorTranslator.FromHtml(&H333333)
    ErrorCodeGrid.RowStyle.BackColor = Drawing.ColorTranslator.FromHtml(&HF7F6F3)
    ErrorCodeGrid.RowStyle.ForeColor = Drawing.ColorTranslator.FromHtml(&H333333)
    ErrorCodeGrid.AlternatingRowStyle.BackColor = Drawing.Color.White
    ErrorCodeGrid.AlternatingRowStyle.ForeColor = Drawing.ColorTranslator.FromHtml(&H284775)
    ErrorCodeGrid.HeaderStyle.BackColor = Drawing.ColorTranslator.FromHtml(&H5D7B9D)
    ErrorCodeGrid.HeaderStyle.ForeColor = Drawing.Color.White
    ErrorCodeGrid.HeaderStyle.Font.Bold = True
    ErrorCodeGrid.HeaderStyle.Font.Size = 8
End Sub

您需要为动态网格指定一个ID,以便它具有ClientID。

    ErrorCodeGrid = New GridView
    ErrorCodeGrid.ID = "ErrorCodeGrid"

编辑:自从你说它对你不起作用以来,你有了更多的见解。

在没有指定ID的情况下,我得到了这个js输出(为了澄清,我删除了一堆你的设计属性):

var GridId = "ctl02"; // Your ID may differ.

和网格视图:

table cellspacing="0" rules="all" border="1" style="border-collapse:collapse;"

请注意,gridview上没有ID规范,所以javascript找不到它,我得到了一个null引用。

添加ID我得到这个:

var GridId = "ErrorCodeGrid";
table cellspacing="0" rules="all" border="1" id="ErrorCodeGrid" style="border-collapse:collapse;" 

请注意,ID匹配。

如果这对你不起作用,也许你使用的是比我旧的asp.net版本。但归根结底,这些ID需要匹配。

如果你不能让它发挥作用,还有另一个选择。将整个代码移动到一个函数中,并传入GridId(无论如何,这是个好主意)。然后可以使用动态网格视图的ClientId动态地注入调用代码。

ClientScript.RegisterStartupScript(Me.GetType, "FreezeHeader", "FreezeHeader(" & ErrorCodeGrid.ClientID & ");", True)

编辑2:使用上面的代码,您需要更改页面中的javascript。

替换此代码:

var GridId = "<%=ErrorCodeGrid.ClientID %>";
var ScrollHeight = 300;
window.onload = function() {

使用此代码:

var ScrollHeight = 300; // you could move this into the function as well.
var FreezeHeader = function(GridId) {

客户端脚本管理器将只输出对此函数的调用。