GridView reference in JScript

Last post 04-16-2007 2:33 PM by goodideadave. 2 replies.

Sort Posts:

  • GridView reference in JScript

    04-16-2007, 12:23 PM
    • Loading...
    • goodideadave
    • Joined on 01-31-2006, 7:11 PM
    • Sunny Sacramento, California
    • Posts 76

    I have a GridView control on my page, and want to loop through the rows and move the data to a window for printing.  But I can’t reference the GridView in JavaScript.

    In my .aspx:

     

    <input type="button" value="Print" onclick="JavaScript:PrintClaimsTab()" />

    In my .js file:

     

    PrintClaimsTab()

    {

    .

      var tableElement = document.getElementById("GridViewClaims");

      for (var i=0;i < tableElement.rows.count; i++)  {

        var rowElem = tableElement.rows[i];

        var tablerow = '<tr>';

        for (var x=0;x < rowElem.cells.count;x++)  {

          var cell = rowElem.cells[x];

          tablerow = tablerow + '<td>' + cells[x].value + '</td>';

        }

        tablerow = tablerow + '</tr>';

        printWindow.document.write(tablerow);

      }

     

    }

     

    But tableElement is always null.

     

    I have also tried:

     

    <input type="button" value="Print" onclick="JavaScript:PrintClaimsTab(<%=GridViewClaims.ClientID%>)" />

    And PrintClaimsTab(GridViewID) { var tableElement = document.getElementById(GridViewID); }

    In which tableElement is also null.

    A different function, which refers to a div, sets tableElement correctly.  Any ideas?

  • Re: GridView reference in JScript

    04-16-2007, 12:58 PM
    Answer
    • Loading...
    • JoshStodola
    • Joined on 01-16-2007, 9:17 AM
    • Heartland of America
    • Posts 3,025

    This should work:

        function PrintClaimsTab(elemId) {
            var tableElement = document.getElementById(elemId);
    
            if(tableElement == null) { alert('Null'); return false; }
    
            for (var i=0;i < tableElement.rows.count; i++)    {
                var rowElem = tableElement.rows[i];
                var tablerow = '<tr>';
                for (var x=0;x < rowElem.cells.count;x++)    {
                    var cell = rowElem.cells[x];
                    tablerow = tablerow + '<td>' + cells[x].value + '</td>';
                }
                tablerow = tablerow + '</tr>';
                printWindow.document.write(tablerow);
            }
        }

     Button:

        <input type="button" value="Print" onclick="PrintClaimsTab('<%=GridViewClaims.ClientID%>');" />
     

    Hope this helps!  Don't forget to mark the most helpful post(s) as Answer for the sake of future readers.  Thanks!

    Josh Stodola ← Come check out my blog!
  • Re: GridView reference in JScript

    04-16-2007, 2:33 PM
    • Loading...
    • goodideadave
    • Joined on 01-31-2006, 7:11 PM
    • Sunny Sacramento, California
    • Posts 76

    That did it.  Thanks very much.

Page 1 of 1 (3 items)
Microsoft Communities
Page view counter