browse by category or date

Printing from a Web programmer perspective is a quite tricky task. Because what we really able to control is on the server side, while the printing is done on the client side.

Unless you create some sort of cool Java applet or custom COM control to help you interface with the printer, you will stuck with Javascript’s Window.print() function.

For those who are stuck without the cool Java applet or the super cool COM control, hopefully this article will help you.

Let’s assume you have the following GridView in your aspx page:

<asp:gridview id="gvCustomersGridView"
        datasourceid="CustomersSource" 
        autogeneratecolumns="true"
        emptydatatext="No data available." 
        allowpaging="true" 
        runat="server">
</asp>

To make this GridView printable, you need to share the clientID to your Javascript printing function. You could either do it by:

  1. previewing the page, copy the ID of the table generated and put it into a variable inside Javascript
  2. Or, you can create a protected variable and put the classic ASP <%= VarName %> inside Javascript

Next, we need to create the print button and the Javascript code to handle the printing.

function Print()
{
	var grid_ID = 'gvCustomersGridView'; //assuming that this is the generated ID
	var grid_obj = document.getElementById(grid_ID);
	if (grid_obj != null)
	{
		var new_window = window.open('print.html'); //print.html is just a dummy page with no content in it.
		new_window.document.write(grid_obj.outerHTML);
		new_window.print();
		new_window.close();
	}
}

You could also comment out the print() and close(), this way user will have a ‘print preview’ and has the chance to set margin, header/footer of the print out.

I hope you find it useful 🙂

About Hardono

Howdy! I'm Hardono. I am working as a Software Developer. I am working mostly in Windows, dealing with .NET, conversing in C#. But I know a bit of Linux, mainly because I need to keep this blog operational. I've been working in Logistics/Transport industry for more than 11 years.

Possibly relevant:

http://ocw.mit.edu/index.html

I am impressed with the vast availability of information that available here. I recommend this site for those who are hunger with Information.

I wonder when my univ follow the steps to provide the lecture notes/tutorials/assignments to everyone. But would that defeat the purpose of people paying such a huge amount of money to pursue their degree? *grin*

About Hardono

Howdy! I'm Hardono. I am working as a Software Developer. I am working mostly in Windows, dealing with .NET, conversing in C#. But I know a bit of Linux, mainly because I need to keep this blog operational. I've been working in Logistics/Transport industry for more than 11 years.

Possibly relevant:

Coffee

I was a bit drowsy this morning in the office. So I quickly went to the pantry and took a cup of coffee from the vending machine. No coins are needed for this vending machine, thank you very much 🙂

After a few sips, I felt much better and started to think what does Wikipedia says about Caffeine. It is an interesting article really. Few things that I noted:

  1. Smoking and drinking coffee IS NOT a good combination. Smoking will shorten the duration of the caffeine effect. So if you drank too much coffee and want to sleep, start puffing!! Bad news for those night watchers which I believe most of them drinking coffee and smoking during the night.
  2. It took at most 45 minutes for the body to completely absorb and distribute the caffeine throughout your body
  3. The caffeine will be active in your body for around 4 hours. It might be longer depends on the age, gender and weight. It is also noted that the effect last much longer for children and pregnant women
  4. There has been success effort to purify caffeine and make them in powder form
  5. Taking too much caffeine might kill you. Estimated around 100 cups of coffee needed. And you must drink this 100s cups in very short time (i.e. the hundredth cup should be drunken when the first cup is still in effect). Of course this is slightly impossible, unless of course, when you snort the powder mentioned before! Announced as the first person who presumed dead because of caffeine is not an achievement you know!

About Hardono

Howdy! I'm Hardono. I am working as a Software Developer. I am working mostly in Windows, dealing with .NET, conversing in C#. But I know a bit of Linux, mainly because I need to keep this blog operational. I've been working in Logistics/Transport industry for more than 11 years.

Possibly relevant: