Javascript GridView冻结标头

Javascript GridView冻结标头,javascript,asp.net,Javascript,Asp.net,我正在使用冻结GridView的标题。我做了教程中解释的所有事情,但是我在IE9中遇到了以下错误,我不知道为什么 错误: 电话号码:182 错误:无法获取属性“offsetWidth”的值:对象为 空或未定义 我在Javascript代码中定义了GridView,如下所示: <script type = "text/javascript"> var GridId = "<%=GridView1 %>"; var ScrollHeight = 300;

我正在使用冻结GridView的标题。我做了教程中解释的所有事情,但是我在IE9中遇到了以下错误,我不知道为什么

错误: 电话号码:182

错误:无法获取属性“offsetWidth”的值:对象为 空或未定义

我在Javascript代码中定义了GridView,如下所示:

 <script type = "text/javascript">
    var GridId = "<%=GridView1 %>";
    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 (var 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>

var GridId=“”;
高度=300;
window.onload=函数(){
var grid=document.getElementById(GridId);
var gridWidth=grid.offsetWidth;
var gridHeight=grid.offsetHeight;
var headerCellWidths=新数组();
对于(var i=0;igridRow.getElementsByTagName(“TD”)[i].offsetWidth){
宽度=头单元格宽度[i];
}
否则{
宽度=gridRow.getElementsByTagName(“TD”)[i].offsetWidth;
}
单元格[i].style.width=parseInt(宽度-3)+“px”;
gridRow.getElementsByTagName(“TD”)[i].style.width=parseInt(width-3)+“px”;
}
parentDiv.removeChild(网格);
var dummyHeader=document.createElement(“div”);
dummyHeader.appendChild(表);
parentDiv.appendChild(dummyHeader);
var scrollableDiv=document.createElement(“div”);
if(parseInt(gridHeight)>ScrollHeight){
gridWidth=parseInt(gridWidth)+17;
}
scrollableDiv.style.cssText=“溢出:自动;高度:“+ScrollHeight+”px;宽度:“+gridWidth+”px”;
scrollableDiv.appendChild(网格);
parentDiv.appendChild(scrollableDiv);
}

那么我如何解决这个问题呢?

您编写的代码不正确

而不是

var GridId = "<%=GridView1 %>";
var GridId=”“;
改为

var GridId = "<%=GridView1.ClientID %>"; //<= Check this

var GridId=”“//是的,你说得对。非常感谢你的帮助。顺便问一下,是否有一个带有冻结标题的水平滚动条?因为GridView标题的宽度现在超出了我放在它里面的特定区域,所以调试
gridWidth
的值。这可能对你有帮助。