Print (print control) in ASP. NET 2.0)

Source: Internet
Author: User

 

How to print in ASP. NET 2.0

In this article, we will explore how to print controls in ASP. NET 2.0. We will print the control in a pop-up window and use the window. Print () method internally. We used Crystal Reports and Js in previous printing. If it was big data, most people would choose Crystal Reports, but it was very troublesome. I used JS a few days ago, however, there are some disadvantages: I printed all the content on my page, including the button. Today I will introduce this control (or a class) to you, which is printed in the specified page area, hope to help you Step 1:Create a printhelper class. This class contains a method named printwebcontrol, which can print any type that you want to control a gridview, DataGrid, panel, text box, etc. You can call window. Print () to simulate the print button.

Note: I have never written this class or I do not know the original author. I will be happy to add a reference, in case someone knows

Using system; <br/> using system. data; <br/> using system. configuration; <br/> using system. web; <br/> using system. web. security; <br/> using system. web. ui; <br/> using system. web. UI. webcontrols; <br/> using system. web. UI. webcontrols. webparts; <br/> using system. web. UI. htmlcontrols; <br/> using system. io; <br/> using system. text; <br/> using system. web. sessionstate; </P> <p> namespace printpage <br/> {<br/> pub LIC class printhelper <br/>{< br/> Public printhelper () <br/>{}</P> <p> Public static void printwebcontrol (Control) <br/>{< br/> printwebcontrol (control, String. empty); <br/>}</P> <p> Public static void printwebcontrol (Control, string script) <br/>{< br/> stringwriter stringwrite = new stringwriter (); <br/> htmltextwriter htmlwriter = new htmltextwriter (stringwrite); <br/> If (Control is webcontrol) <br/>{< br/> unit W = new unit (100, unittype. percentage); <br/> (webcontrol) control ). width = W; <br/>}< br/> page Pg = new page (); <br/> PG. enableeventvalidation = false; <br/> If (script! = String. empty) <br/>{< br/> PG. clientscript. registerstartupscript (PG. getType (), "printjavascipt", script); <br/>}</P> <p> htmlform FRM = new htmlform (); <br/> PG. controls. add (FRM); <br/> frm. attributes. add ("runat", "server"); <br/> frm. controls. add (control); <br/> PG. rendercontrol (htmlwriter); <br/> string strhtml = stringwrite. tostring (); <br/> httpcontext. current. response. clear (); <br/> httpcontext. current. response. write (strhtml); <br/> httpcontext. current. response. write (""); <br/> httpcontext. current. response. end (); <br/>}< br/>} 

 

Step 2:Create two pages: default. aspx and print. aspx.
The default. aspx page contains the control to be printed. Print. aspx will call the print function as a pop-up page.

 Step 3:In your default. aspx, drag and drop several controls to put all the content and controls you want to print into a container (panel ).
In this way, we can print the container.

Step 4:Add the print button default. aspx Page code and type the following content:

Protected void btnprint_click (Object sender, eventargs e) <br/>{< br/> session ["control"] = Panel1; <br/> clientscript. registerstartupscript (this. getType (), "onclick", "window. open ('print. aspx ', 'printme', 'height = 300px, width = 300px, scrollbars = 1'); "); <br/>} 

 

Step 5: In the print. aspx. CS page_load event, add the following code:

<Br/> protected void page_load (Object sender, eventargs e) <br/>{< br/> control = (Control) session ["control"]; <br/> printhelper. printwebcontrol (control); <br/>}< br/> 

 

That's it. I hope this article is useful. Thank you for watching it.

 

 

Code download: http://www.51ascx.com/183.html

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.