2

I have a USER CONTROL with a <div> tag and a button. I want to print the content of the <div> when the button is clicked. Only the content of the div and nothing else no matter where the control is used. This without opening a new window. I cannot find a solution that works with every browser!

Thank you!

Uince
  • 193
  • 3
  • 5
  • 11
  • When you say print the content of the div, you mean you want to make like a printer-friendly page with only the contents of the div? – wsanville Feb 02 '10 at 14:39

3 Answers3

4

You might be able to generate a CSS file that excludes everything except the <div>, something in the lines of:

<link rel="stylesheet" type="text/css" media="print" href="print.css" />

And the putting something like this in print.css:

* { display: none; }
#specialdiv { display: block; }
kb.
  • 1,955
  • 16
  • 22
0

In the server-side click handler of the button, send out the content you want to display:

protected void MyButton_Click(object sender, EventArgs e) {
    Response.Clear();
    Response.Write(...the content of your div; be nice and add at least some html headers...);
    Response.End();
}

Clear removes everything from the output buffer, Write outputs your stuff and End stops processing the rest of your page.

Heinzi
  • 167,459
  • 57
  • 363
  • 519
0

As kb said, use css to define what is and what isn't printed on a page. If you want a print button which prints just a div follow kb's instructions. If you still want file->print to behave as normal however, change the page's (media="print") css prior to calling window.print() and revert it afterwards in the button's onclick function.

Sorry to be the bearer of bad news but there isn't a comprehensive print api for javascript!

Martin Booth
  • 8,485
  • 31
  • 31