冻结动态网格的标题行

时间:2012-08-25 18:29:41

标签: javascript asp.net vb.net gridview

我有一个动态创建的gridview。我在运行时将它添加到我的表单中(所以我的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

1 个答案:

答案 0 :(得分:0)

您需要为动态网格指定ID才能使其具有ClientID。

    ErrorCodeGrid = New GridView
    ErrorCodeGrid.ID = "ErrorCodeGrid"

编辑:因为你说它不适合你,所以更多的见解。

如果没有指定ID我得到了这个js输出(我删除了一堆你的设计属性以便澄清):

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

和gridview:

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

请注意,gridview上没有ID规范,因此javascript找不到它,我得到一个空引用。

添加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) {

客户端脚本管理器将输出对此功能的调用。