February 24, 2009...9:54 am

How to Print a Gridview in ASP.NET 2.0

Jump to Comments

“”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"

<script language=”javascript” type=”text/javascript”>
function getconfirm()
{
return confirm(‘Are you sure you want to change the Folio No?’);
}
function Button1_onclick() {
//open new window set the height and width =0,set windows position at bottom
var a = window.open (”,”, ‘ left = ‘ + screen.width + ‘,top=’ + screen.height + ‘,width=0,height=0,toolbar=0,scrollbars=0,status=0′);
//write gridview data into newly open window

//major change here get innerHTML Of the Div

a.document.write(document.getElementById(‘innerData’).innerHTML);
a.document.close();
a.focus();
//call print
a.print();
a.close();
return false;
}

function IMG1_onclick() {
return Button1_onclick()
}

</script>

“”"”"”"”"”"”"”"”"”"”"”"”"”"”"”"

Here the gridview must be placed in a DIV Tag
===================================

<div id=”innerHTML”>

Place Gridview Control here

</div>

<asp:ImageButton ID=”ImageButton1″ ImageUrl=”~/images/Printer-512×512.png” Width=30 Height=30 runat=”server” OnClientClick=”return Button1_onclick()” />

2 Comments

  • Good posting. I’m trying to print my GridView data using this code. But can’t
    here is my code:

    function printChart(){
    //Get chart from its ID
    var chartToPrint = getChartFromId(“MonthlyEventTotal”);
    chartToPrint.print();
    }

    function GridViewPrint()
    {
    var a = window.open (”,”,’left =’ + screen.width + ‘,top=’ + screen.height + ‘,width=0px,height=1px,toolbar=0,scrollbars=0,status=0,resizable=no’);

    a.document.write(document.getElementById(‘DivToPrint’).innerHTML);
    a.document.close();
    a.focus();
    a.print();
    a.close();
    return false;
    }

    <%—-%>

    <%– –%>
                                 

      

    2005
    2006
    2007
    2008
    2009
    2010

      

     

                       
      

    <%– –%>
    <%– –%>
         
                               
     
               

     

    <asp:Label ID=”lblEMonth” runat=”server” Text=”>

     

    <asp:Label ID=”lblEName” runat=”server” Text=”>

     

    <asp:Label ID=”lblTotal” runat=”server” Text=”>

     

    <asp:Label ID=”lblComments” runat=”server” Text=”>

     
     
                     
                           
                             
                  

    I called the JavaScript function in my Page_Load event:

    protected void Page_Load(object sender, EventArgs e)
    {
    btnPrint.Attributes.Add(“onclick”, “GridViewPrint()”);

    }
    when I click btnPrint no print out is generating.

    pls need help.

    thanks in advance

    Maksuda…


Leave a Reply