Discussion on web printing methods based on ASP. NET 2.0

Source: Internet
Author: User

AbstractAbstract: web printing is used to develop B/S structure applications.ProgramIs a complex, widely used, and very practical technology. This article mainly discusses several methods for implementing web printing in ASP. NET 2.0, and describes the implementation and features of each method.

Key words: ASP. NET 2.0; B/S mode; Web Printing

Graph classification: tp393.092           Document ID:

Study on Implementation of Web Print Method Based on ASP. NET 2.0

             Zhu Xiao-dong

(Tianjin Railway Technical and Vocational College, Tianjin 300240)

Key words:ASP. Network 2.0; B/S Mode; Web Print

Currently, the B/S mode is widely used in software development practices because of its good scalability, high data security, and client compatibility, web Printing is an issue that must be considered in B/S mode applications. Although the client rules are simple, easy to learn, easy to maintain, and easy to publish, the client printer cannot be precisely controlled because the browser can only be used as the user interface to interact with the server, this makes it difficult to process complex printing such as Web Report Format control and customization. the particularity of its structure is different from the printing function in general programs, software developers always strive to make the B/S system have a print function similar to the C/S system, which is a common problem faced by developers.

Web Printing is usually divided into client-side scripting and server-side scripting. In client-side scripting, JS can be used to analyze the content of the Source Page and extract the page elements to be printed, print. By analyzing the content of the source document, you can generate a printed target document. In this way, the client generates the printed target document independently to reduce the server load. However, the analysis of the source document is complex and the printed content in the source document must be agreed. Generally, the server-side program is used, and the backgroundCodeRead the print source from the database and generate the printed target document. When the page is generated, you should also consider using CSS to implement forced paging control. In this way, you can generate a rich set of printed target documents, which are highly controllable. Since the printed content is obtained from the database, the generation operation is relatively simple, but the server load is relatively large.

ASP. NET 2.0 is a major component of Microsoft's new architecture. NET Framework 2.0, which has greatly improved the efficiency, management, performance, and scalability of developers. Visual Studio 2005, the newly developed IDE, is currently the main development tool for B/S mode applications. Web printing methods for ASP. NET 2.0 mainly have the following solutions:

① Web printing using IE browser;

② Call office to implement web printing;

③ Web printing using third-party controls;

④ Use the. NET 2.0 component crystal report for web printing.

1 Use IEWeb BrowserPrint

1.1 call the windows. Print () method for Printing

Add the print button on the page and add the following code:

<Input type = "button" onclick = "window. Print ()" value = "print">

The code in this method is simple, convenient, and convenient, without any settings, but other elements on the page will be printed and processed, and the format of the page header and footer is difficult to control, "Print Settings" and "print preview" cannot be implemented, and media in CSS can be usedTo set the print and display formats. The Code is as follows:

<StyleMedia = "print"Type = "text/CSS">

. Noprint {display: none ;}

. Pagenext {page-break-after:Always ;}


Add the following elements on the page that you do not want to print:Class = "noprint"Then it will not appear in print or print preview. For example:

& Lt; table width = "757"Height = "174"Border = "0"Align = "center" cellpadding = "0" cellspacing = "0">

<TrClass = "noprint">

<TD Height = "133" align = "center" valign = "TOP">




In this case, <tr class = "noprint"> is a non-printable tr.

1.2 call the webbrowser Control for Printing

The webbrowser control is an embedded browser control of IE. It is an ActiveX control and can be printed by calling the execwb method of the webbrowser control. First, an object is created in the HTML source code, the object mainly inserts an object into the HTML page. The Code is as follows:

// Add the webbrowser control with the ID of webbrowser

<Object ID = ''webbrowser ''width = ''0''Height = ''0''

Classid = ''CLSID: 8856f961-340a-11d0-a96b-00c04fd705a2'' viewastext> </Object>

The classid attribute is used to obtain or set the Class Identifier of an object. Finally, in The onclick event of the client control, call the relevant methods of the webbrowser component to print, print, preview, and perform other operations. The Code is as follows:

<Input type = buttonValue = print onclick = Document. All. webbrowser. execwb (6, 6)>

<Input type = buttonValue = page setting onclick = Document. All. webbrowser. execwb (8, 1)>

<Input type = buttonValue = print preview onclick = Document. All. webbrowser. execwb (7,1)>

The advantage of this method is that you can easily print, set pages, and print previews. The client can print the target document independently to reduce the load on the server. The disadvantage is that the analysis operations of the source document are complex, the printed content in the source document must be restricted.

Call officeWeb implementationPrint

When browsing a webpage, you often need to print the content on the Web page through word or Excel documents. with powerful office software, you can complete complicated document formats, fine-grained control, adaptable and easy to control. The following describes how to export data to a Word document. Excel is similar to this.

Word, as a document processing software, shows powerful functions in the process of processing documents and materials. Exporting data to word is mainly achieved by setting the contenttype attribute of the response object to the type of the output file. The Code is as follows:

Response. appendheader ("content-disposition", "attachment?filename=test.doc ")

Response. contenttype = "application/MS-word ";

// If the output is Excel, modify it to the following code:

// Response. appendheader ("content-disposition", "attachment=filename=test.xls ");

// Response. contenttype = "application/MS-excel ";

This. enableviewstate = false;

String write Tw = new string writer ();

Htmltextwriter hW = new htmltextwriter (TW );

Gridview1.rendercontrol (HW );

Response. Writer (TW. tostring ());

Response. End ();

After being exported to word or Excel, you can customize the printed content and format. The development cycle is short, the maintenance cost is low, the user is convenient to use, and cross-platform operation is supported. However, it is difficult to generate a print preview before data export. When using the server or client, ensure that the office software is installed, when using the client, the client must have certain requirements on the security settings of IE.

3 Printing using third-party controls

Currently, many developers provide related controls to implement web printing functions, such as Max reports, Ruyi reports, and scriptx. Excellent third-party controls, flexible printing methods, web applications, short development cycles, and good system maintenance. However, the printing function is restricted by the control itself. Generally, it cannot be cross-platform, and the relevant fees must be paid, which increases the system development cost.

You can also develop and implement web printing by yourself. Generally, you can develop ActiveX controls, Java applets, or dynamically generate PDF files. This method is highly customizable and allows you to design web pages according to user requirements. However, the development cycle is long and the development and maintenance costs are high.

Currently, some software companies use Java to develop web-based reports. When using these forms, they must first install them on the client and then generate VBScript scripts on the server, run on the client and create the corresponding component object. Print the component object. This method is only required after the COM component of the client is developed. As long as you are familiar with VBSCRIPT or lavascript script language, you can easily call it. This method features high printing efficiency, rich functions, precise positioning and printing, and low installation cost. ActiveX is more than 1 MB. It must be installed once when the client is used for the first time. However, after all, the client needs to install components, which is not easy to deploy. The browser version is also required.

4 Print with Crystal Reports

Crystal report is an excellent report development tool. It is favored by developers for its simplicity, ease of use, and powerful functions. CrystalReportsFor Visual Studio. NET 2005 has the following features:

① For all. NET provides high integration for the target programming language, which can be used in Windows applications or ASP.. NET web applications use the programming language of their expertise to design reports.

② The powerful report design tool crystalreportdesigner can be used to define report data sources, locate various controls, and easily create reports in various complex formats.

③ Use the report viewer control (crystalreportviewer) to set the report format and custom export options, and display the Report on the web form page.

④ You only need a web browser to view reports.

Crystal report printing is flexible and convenient, and the printing content is rich and colorful. You only need a web browser to upload reports. Users can export the reports to various formats, such as. Documents,. doc,.xls,.html ,. RPT and so on, users not only can easily interact with reports, but also have powerful functions such as content creation, computing, grouping, and query.

Crystal Reports for Visual Studio. NET 2005 has two execution modes: Pull model and push model. You can obtain Crystal Reports data in any mode. In the PULL model, the driver connects to the database and pulls in the data as needed. When using this model, the connection to the database and the SQL commands executed to obtain data areReport Processing does not require developers to write code. Therefore, if you do not need to write any special code at runtime, use the PULL model. On the contrary, the pushing model requires developers to write code to connect to the database, execute SQL commands to create a record set or dataset that matches the fields in the report, and pass the object to the report. This method can place the connection share in the application and filter the data before Crystal Reports receives the data.

In Visual Studio 2005, you can use the crystalreportviewer control to view data in the report source and design the report to make the report more beautiful.

5 Conclusion

To sum up, this article describes ASP. net web printing technology is initially discussed, and the advantages and disadvantages of each implementation method are briefly introduced. In the development process, users should select according to the actual needs of the project, flexible processing of various problems encountered by web printing to achieve better application results.



[1] example of ASP. NET program development [M]. Beijing: People's post and telecommunications Press, 2007: 572-599.

[2] Li Minggang, Xiao Jian. ASP. NET (C #) Basic tutorial [M]. Beijing: Tsinghua University Press, 2006: 294-392.

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.