none
Problemas con JavaScrip en Gridview RRS feed

  • Pregunta

  • Buen día mi problema es el siguiente, tengo un código JS para aplicar un encabezado fijo a mis gridview (2), el problema es que cuando se muestro mis grid (por separado) solo se le aplica al GridView2, alguna sugerencia por favor, necesito que ambos tengas el encabezado fijo, el código es el siguiente;

        <script type="text/javascript">
    
            var GridId = "<%=GridView1.ClientID %>";
            var ScrollHeight = 450;
    
            window.onload = function () {
    
                var len = $("#GridView1").length;
                if (len != 0) {
                    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>
    
            <script type="text/javascript">
    
                var GridId = "<%=GridView2.ClientID %>";
                var ScrollHeight = 450;
    
                window.onload = function () {
    
                    var len = $("#GridView2").length;
                    if (len != 0) {
                        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>

    lunes, 28 de agosto de 2017 18:11