How to implement custom and automatic page-page printing DataGrid display

zhaozj2021-02-08  228

Home:

http://www.maxss.net

Email: Maxchou@163.com

Since Microsoft launched the .NET platform in 2001, the application development of the BS structure in the network era has reached a new climax. And .NET This model drives the WEB development mode to bring the original BS development model to a new field, making programmers and developers more efficiently and quickly develop and deploy more secure enterprise applications. Although this new generation of development platforms have various advantages, there are still some places that are not perfect and unreasonable. But a little sure is that Microsoft will continue to constantly inject new motivation for its injected source.

Now returning to the topic of this article, currently printing in the web environment is a necessary feature for the MIS system, in general, direct printing or exporting of IE or exports printing again (such as Word format) . However, customers' needs are always changed, and in some time, I have to increase new printing functions in their products in a company based on .NET platform development OA products. Demand is of course from the customer, the customer's core requirements are the data displayed by the DataGrid (of course, not as simple, then look). Requirement 1: For the DataGrid specified to be printed, you can control the record of the specific number of specifics per page; Requirements 2: If the record number exceeds a page according to the previous condition, you must implement the page-by-page automatic printing, no artificial intervention; request three : You can customize the report header information (print date, title, etc.) and format control (font size, alignment, table display style, etc.).

When I received this demand, I started to ponder. It is not very difficult to implement these functions in the application, but it is not a relaxed thing in the web environment, let alone customer requirements are so harsh. The most critical problem is to solve the requirements of one and requests. In fact, they can integrate the issues mentioned in the article title, how to implement custom and automatic page-page printing DataGrid display.

I hope that I can seek a solution by using new .NET technology to find ways to discuss Cystal Reports, PrinterController and PrintDocument, these technologies are not very mature and appropriate. . After the crystal report, it can generate a report (such as DOC, PDF) to the client (such as DOC, PDF) to print again; for PrinterController and PrintDocument, it can only be applied to the server side, which seems to be more difficult to implement.

After a few days of hard work, I found that only one of the programs can be trial, that is, using the JavaScript WebBrowser control to implement printing at the client. But the problem is now coming out, how do you operate the data brought by DataGrid through JavaScript? If you want to make a page of page a page, you don't have to develop this feature. You can easily complete it through the print menu of IE. Now .NET DataGrid can easily implement pagination display, we can use this ready-made function to think, so the remaining key issues are how to automatically transfer the DataGrid to other pages. First of all, of course, it is to analyze the HTML code generated by the client. Later, I found an important breakthrough in a bunch of dense Ma Ma, my own understanding is that the trigger of the so-called server-side event is basically through the client. The page generates a specific JavaScript function and implemented by interaction with the user. These functions are

JavaScript: __ dopostback ('first-level control name: _ level control name ...', ''), etc. format, of course, including the page number when displayed in the DataGrid below, is there. Each page number in the NET is treated as a three-level control. The names of these controls are automatically generated by the system, name format, such as "_ctl0, _ctl1, _ctl3 ...". It is now assumed that the data binded to the DataGrid is divided into three pages, which corresponds to the basic shape of the JS script generated in the client:

First: JavaScript: __ dopostback ('datagrid1: _ctl11: _ctl_2: _ctl0', '')

Page II: JavaScript: __ dopostback ('DataGrid1: _ctl11: _ctl_2: _ctl1', '')

Third Page 3: JavaScript: __ dopostback ('datagrid1: _ctl11: _ctl_2: _ctl2', '')

So our problem is clear, as long as the script corresponding to the page can dynamically generate other subsequent scripts and execute, so we can freely and flexibly control the switching of DataGrid in each paging.

Next, we must solve the second problem, how do you display information in the DataGrid? I have seen some other system practices to reopen a new page, and then perform a database read operation to generate the content to print. Such efficiency is analogously, more, more than data displayed in .NETK is obtained by binding, and in many cases the user filtering some fields, if the old practice is obvious It is unrealistic and difficult to implement. Since the data to be printed is the data displayed in the DataGrid, why do we use them directly? The same processing method, DataGrid is generated to display data at the client, and their ID attribute is the Name property in the design phase DataGrid. This way we can take out the OuterHTML value of this table by JavaScript is what it is currently displayed. You can implement hidden printing in the Document object of