views:

366

answers:

3

Hi, 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!

+3  A: 

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 This is exactly what I was going to suggest.
Byran
A: 

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
A: 

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