Thursday 3 May 2012

Print Out Only Grid View Or Any Fix Content Area In Asp.net Page

Print out only Grid view,Datalist,Detail view or Any fixed content area like <div> in asp.net page

Some time you have so many things in your single page lets say a GRIDVIEW,a DATALIST,a DIV ,or any other(server control which has a ID)from which you want to print the individual content and not the whole page. From this i mean how to print a fix content area from whole page in asp.net,lets see how easly it could be done.



Just take a look at below webpage .
Now as a developer how could you  print out only red marked areas with a print button click.








In case you need help lets proceed with this tutorial...

Lets take brief look at simple screen below

 






Its simply include
a div with id name printable and runat="server"
then my text under that div
and some text outside that div


 Here div is server control  as i made it by giving it a id name and runat="server" as below

<div runat="server" id="printable">
          
 </div>




Similarly it could be any control either Datalist,Gridview or any Control
Now in final there is image button...(you don't have to fire its click event!)


Just write this code in Page_Load 
and at the time you click it ,Our page will automatically recognize it and give you the print out option!


 protected void Page_Load(object sender, EventArgs e)
    {
            string printScript =@"function PrintGridView()
         {
            var gridInsideDiv = document.getElementById('printable');
            var printWindow = window.open('gridview.htm','PrintWindow','letf=0,top=0,width=850,height=300,toolbar=1,scrollbars=1,status=1');
            printWindow.document.write(gridInsideDiv.innerHTML);
            printWindow.document.close();
            printWindow.focus();
            printWindow.print();
            printWindow.close();}";
            this.ClientScript.RegisterStartupScript(Page.GetType(), "PrintGridView", printScript.ToString(), true);
            btnPrint.Attributes.Add("onclick", "PrintGridView();"); 
   }


and finaly the sample code is here...



1 comment:

Print Only Grid View in ASP.net

ASP.net How to Print Only GridView < div id ="gridviewDiv" >   < asp:GridView ID ="gridViewToPri...