Responsive Ads Here

Saturday, May 4, 2013

Print all the records in the grid view even paging enabled using asp.net and java script

One of the most common functionality in any ASP.NET application is to print forms and controls. There are a lot of options to print forms using client scripts. In the article, we will see how to print controls in ASP.NET using both server side code and java script. In my latest project i have a requirement to print a grid view contents. Generally when we are printing a page using java script, only the displayed content will be print. I mean in my page i have grid view which has paging, grid view has 25 records we are displaying 5 records per page. Then grid view has total 5 pages. when we are printing the panel which contains grid view the first 5 records will be print. some times we have a requirement print all the records in that grid view. Below script will help us to print a div or panel.
 
function PrintPanel() {  
     var divContents = $(".ContentDiv").html(); //ContentDiv means div or panel class name   
     var printWindow = window.open('', '', 'height=400,width=1050,scrollbars=1');  
     printWindow.document.write('Business Trends Report');  
     printWindow.document.write(''); //css path  
     printWindow.document.write('');  
     printWindow.document.write(divContents);  
     printWindow.document.write('');  
     printWindow.document.close();  
     printWindow.print();  
     EnablePaging(); //  
   }  
 function EnablePaging()  
   {  
    document.getElementById('<%=imgGetReport.ClientID %>').click();  
   } 

Here use of enable paging is, basically when user clicks on the print button in code behind we are disabling the paging of grid view and binding the grid view again.After that we will call the java script function from code behind like below.

 
protected void btnPrint_Click(object sender, EventArgs e)  
     {  
       try  
       {  
         if (Session["PaymentDetailsDatasource"] != null)  
         {  
           DataSet ds = (DataSet)Session["PaymentDetailsDatasource"];  
           if (ds.Tables[0].Rows.Count > 0)  
           {  
             DetailsGrid.DataSource = ds;  
             DetailsGrid.AllowPaging = false;  
             DetailsGrid.DataBind();  
             ScriptManager.RegisterStartupScript(Page, this.GetType(), Guid.NewGuid().ToString(), "javascript:PrintPanel()", true); //javascript calling  
           }  
         }  
       }  
       catch (Exception ex)  
       {  
           
       }  
     }  
So the java script method will print all the records, because now the grid view has no paging so all the records will be printed. But after giving the print user will be able to see the paging the grid view. so we are calling enable paging method, in that method i am explicitly calling the click method of button. In that click method i am enabling the page. This is the trick to print all the records in grid view while paging is enabled in grid view.

No comments:

Post a Comment