0

我有一个动态创建的gridview。我在运行时将它添加到我的表单中(所以我的 HTML 源代码没有标签)。我设法获得了滚动条,但我的经理要求我在滚动时冻结标题行。互联网上的详尽搜索仅显示了静态网格视图示例。我尝试实现一个通用的 javascript 解决方案,但收到错误“需要对象”标记此行:

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
4

1 回答 1

0

您需要为动态网格指定一个 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 找不到它,我得到一个空引用。

添加我得到的ID:

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

请注意,ID 匹配。

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

如果您无法使其正常工作,则另一种选择。将整个代码移动到一个函数中并传入 GridId(无论如何这是个好主意)。然后就可以用动态gridview的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) {

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

于 2012-08-25T23:23:05.050 回答