asp.net - print div content from user control without opening a new window - Stack Overflow

I have a USER CONTROL with a <div> tag and a button. I want to print the content of the <div&g

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!

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!

Share Improve this question asked Feb 2, 2010 at 14:35 UinceUince 1933 gold badges5 silver badges11 bronze badges 1
  • 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 Commented Feb 2, 2010 at 14:39
Add a ment  | 

3 Answers 3

Reset to default 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; }

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.

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 prehensive print api for javascript!

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745611549a4635975.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信