最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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!

发布评论

评论列表(0)

  1. 暂无评论