By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,991 Members | 1,887 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,991 IT Pros & Developers. It's quick & easy.

how to use javascript for all grids

P: 62
Expand|Select|Wrap|Line Numbers
  1. <script type = "text/javascript">
  2.     var GridId = "<%=GridView1.ClientID %>";
  3.     var ScrollHeight = 300;
  4.     window.onload = function () {
  5.         var grid = document.getElementById(GridId);
  6.         var gridWidth = grid.offsetWidth;
  7.         var gridHeight = grid.offsetHeight;
  8.         var headerCellWidths = new Array();
  9.         for (var i = 0; i < grid.getElementsByTagName("TH").length; i++) {
  10.             headerCellWidths[i] = grid.getElementsByTagName("TH")[i].offsetWidth;
  11.         }
  12.         grid.parentNode.appendChild(document.createElement("div"));
  13.         var parentDiv = grid.parentNode;
  14.  
  15.         var table = document.createElement("table");
  16.         for (i = 0; i < grid.attributes.length; i++) {
  17.             if (grid.attributes[i].specified && grid.attributes[i].name != "id") {
  18.                 table.setAttribute(grid.attributes[i].name, grid.attributes[i].value);
  19.             }
  20.         }
  21.         table.style.cssText = grid.style.cssText;
  22.         table.style.width = gridWidth + "px";
  23.         table.appendChild(document.createElement("tbody"));
  24.         table.getElementsByTagName("tbody")[0].appendChild(grid.getElementsByTagName("TR")[0]);
  25.         var cells = table.getElementsByTagName("TH");
  26.  
  27.         var gridRow = grid.getElementsByTagName("TR")[0];
  28.         for (var i = 0; i < cells.length; i++) {
  29.             var width;
  30.             if (headerCellWidths[i] > gridRow.getElementsByTagName("TD")[i].offsetWidth) {
  31.                 width = headerCellWidths[i];
  32.             }
  33.             else {
  34.                 width = gridRow.getElementsByTagName("TD")[i].offsetWidth;
  35.             }
  36.             cells[i].style.width = parseInt(width - 3) + "px";
  37.             gridRow.getElementsByTagName("TD")[i].style.width = parseInt(width - 3) + "px";
  38.         }
  39.         parentDiv.removeChild(grid);
  40.  
  41.         var dummyHeader = document.createElement("div");
  42.         dummyHeader.appendChild(table);
  43.         parentDiv.appendChild(dummyHeader);
  44.         var scrollableDiv = document.createElement("div");
  45.         if(parseInt(gridHeight) > ScrollHeight){
  46.              gridWidth = parseInt(gridWidth) + 17;
  47.         }
  48.         scrollableDiv.style.cssText = "overflow:auto;height:" + ScrollHeight + "px;width:" + gridWidth + "px";
  49.         scrollableDiv.appendChild(grid);
  50.         parentDiv.appendChild(scrollableDiv);
  51.     }
  52. </script>
  53.  
I am using above script to lock/freeze headers of asp grid view it works but it works for only one grid
i want to make one (above) script work for all the grids which are web page.

how can i do it
please be specific i am totally bad at java script
Jan 23 '15 #1
Share this Question
Share on Google+
1 Reply


gits
Expert Mod 5K+
P: 5,270
obviously you assign exactly one id of a grid on that page to the variable GridId. to have that for all grids - you would need to retrieve all grid's ids and use a loop to apply the desired behaviour to make it work with all grids on your page.
Feb 18 '15 #2

Post your reply

Sign in to post your reply or Sign up for a free account.