Create multi-page Custom reports using ASP. NET

Source: Internet
Author: User
Create multi-page Custom reports using ASP. NET
Release date: 4/1/2004 | Updated on: 4/1/2004

Chandra kamalakantha

EDS Corporation

Marius rochon

Microsoft Corporation

September 2002

Applicable:

Microsoft ASP. NET

Microsoft Visual Studio. NET

Abstract:This article describes how to generate simple and complex multi-page HTML reports using the existing ASP. NET infrastructure (designer, data binding, and other runtime functions. (7 page printing)

Download reporttool.exe.

Content on this page

Introduction
Our method
Use Data Binding to create a report
Runtime
Summary
Thank you
About the author

Introduction

This article describes how to generate simple and complex reports using the existing ASP. NET infrastructure-designer, data binding, and other runtime functions. A report consists of HTML that contains multiple pages of data. In general, Microsoft ASP. NET is used to process a single page form with a set of UI controls. For the purpose of reporting, we need ASP. NET to generate a recurring sequence of the same HTML output until all report data is exhausted. To achieve this goal, we have extendedSystem. Web. UI. PageClass. Specifically, we have rewrittenRenderMethod and some other design-Time Attributes are added. With this new class, developers can use the standard Microsoft Visual Studio.. Net Form Designer to set the layout of the report page, set its attributes, and the properties of each control contained in it, execute the project to generate an HTTP Response stream that is bound to each continuous component of the incoming data and has the same layout.

An output example generated by this report is included in the report.htm file.

We have extended the features of this new class to allow multiple input data streams and use multiple optional page la s in the same report (for example, the first page, report total page, details page, and can browse the entire report page by page (instead of repeatedly accessing the server ). All relatedCodeAre included in the reportclass project. The msdnarticletest project contains sample report Definitions using this framework.

Back to Top our method

ASP. NET supports the rendering of one page (HTML form) in the form of immediate availability. Therefore, you can use it to respond to each page request in the report to present the page. However, this requires user intervention to generate the entire report and maintain the cursor status between different accesses to the server. If you want to generate the report as a whole, this may be an expensive requirement.

However, ASP. NET also provides a design and runtime scalable framework, which allows us to change its default behavior. In this method, we have used a new class (Reportpage).System. Web. UI. PageClass (by default, ASP. NET forms are derived from this class.

ReportThe basic operations of the class are reflected in itsRenderMethod, this method is overwritten (and replaced)System. Web. UI. PageInRenderMethod:

First, skipFormMark the relevant HTML generation process-this is unnecessary and will actually interfere with the reuse of other control IDs. Load a dataset that fills the report page with enough data (usingDataGridPage size ). Next, callDatabindTo associate data with all controls on the page. Use the standard of each controlRenderFirst, load the dataset and repeat this operation until no data is to be loaded. FinallyRenderMethod.

Back to Top: Use Data Binding to create a report

The example report we will create (as described below) provides the following features of the report:

    • One multi-page report for viewing all authors (Steps 1 to 13)

    • When the author's status changes, perform paging (Step 14)

    • Total number of trails by zip code (Step 15)

    • List all functions that can be rewritten

    • Show how to add page navigation to the report (step 17)

Use Data Binding to create a report

  • 1. Create a new solution named "customreporting ".

  • 2. Create a new C # project of the class library type and name it "reportclass ".

  • 3. Delete the class1.cs module.

  • 4. Download the attached reportpage. CS module to your computer.

  • 5. Click the reportclass project and clickAdd existing item, And thenReportpage. CSAdd to this reportclass project.

  • 6. Add a reference to system. Data and system. web in the reportclass project.

  • 7. Generate a class library to prepare for using data binding to create a report.

  • 8. Add a new ASP. NET web project to customreporting solution and name it "reportwritertest ".

  • 9. Add a reference to reportclass under reportwritertest. (ClickReferences, InProjectsTab, selectReportclassProject .) Contains reportclass. dll (displayed onSelected componentsPart ).

  • 10. AddReportclass, Edit webform1.aspx.

  • 11. Set

    Public class webform1: system. Web. UI. Page

    Change

    Public class testreport: reportclass. reportpage

    This forces the web page fromReportclass. reportpageTo test the web page.ReportpageClass (ReportclassThe public attributes and methods.

  • 12. Generate the reportwritertest project.

  • 13. Edit webform1.aspx In the designer and design the report layout. In this example? Generate Reports.

    Edit Webform1.aspxDesign the report layout

    • A. Add an HTML table with two rows and four columns.

    • B. Edit HTML and deleteStyleAttribute.

    • C. SetCellspacing,CellpaddingAndBorderSet to 0.

    • In the first row of the table, add two tag controls. The first label is usedPageLabel, and the second label will display the page number; therefore, it will be named "lblpage" and the text should be set as a space.

    • E. Add two tag controls in the second row of the table. The first tag control is used to label the status. The second tag control will be named "lblstate" and its text will be setEmpty spaces.

    • F. on the third row, delete the three columns in the four columns, with only one column left. Set the column width to the same as the table width.

    • G. Drag a data-bound grid control in an HTML table to the third row.

    • H. Edit the data binding grid column set and set four columns for the author's surname, name, address, and city. ExploitationBound ColumnThe total number of pages is calculated based on the zip code. Note: During running, deselectCreate columns automatically.

    • I. Add two other labels on the fourth line. The text of the first label will be setTotalThe text of the second label is setEmpty spacesAnd it will be named "lbltotal ".

    • J. embed the entire table into a panel and specify IDPage1. Make sure that the Panel is set to run on the server.

    • K. Drag a non-typed dataset to the form.

    • L. NavigatePropertiesTab and selectWebform1.aspxPage. SetDatasetSet the property to the dataset on this page. SetData-boundSet the grid to the data binding grid on the page.

    • M. Fill in the dataset in the page_load event on the web page.

    • N. include the following code to fill the dataset:

       private void page_load (Object sender, system. eventargs e) {If (ispostback) return; string sconnectionstring = "provider = sqloledb; Data Source = .; initial catalog = pubs; user id = sa; Password = *** "; string ssql =" select au_lname, au_fname, address, city, state, zip from authors order by state "; oledbconnection dbcon = new oledbconnection (sconnectionstring); oledbcommand dbcmd; oledbdataadapter dbadapt = new oledbdataadapter (); try {dbcon. open (); dbcmd = new oledbcommand (ssql, dbcon); dbadapt. selectcommand = dbcmd; dbadapt. fill (dataset1); dbadapt. dispose (); dbcmd. dispose ();} catch (exception E1) {response. write ("database error" + e1.message);} finally {dbcon. close (); dbcon. dispose () ;}
  • 14. Set the page group/group separator Based on the column:

    Base. trackcolumnbreaks ("one", "State"); this will provide an override function and exposes the row that accounted for column/Page Break. protected override void processpagebreak () {lblstate. TEXT = currentdatarow ["state"]. tostring ();}
  • 15. Set and track the total number:

    Base. tracktotals ("Zip", reportclass. totalsscopeenum. Page); base. tracktotals ("Zip", reportclass. totalsscopeenum. Report );
  • 16. Other rewrite functions include:Processpagebreak,Postprocesspagebreak,Processgroupbreak,Postprocessgroupbreak,ProcessreportendAndProcesscurrentrow. Do not rewriteProcesscurrentrow. Developers can rewrite these functions to reset or print content based on actual conditions.

  • 17. To add a navigation bar to a report, include pagenav. js in the project and include it on each report page. Include viewreport.aspx1_reportdetail.htm and reportheader.htm in this project. Showreport. aspx contains an example to illustrate how to call viewreport. aspx for page navigation. The only action completed by this navigation function is to move between various links (for example, http: // localhost/msdntest/webform1.aspx # page [1-N.

Back to Top running

What happens when you execute this report?ReportThe actual operation of the class is in itsRenderMethod, this method overwrites the standardPageClass.

Starting HTML Tag is through callingControls [0]. RenderMethod. At a stage,Control. RenderCallPage. verifyrenderinginserverform. The default implementation method of this function will cause an exception. ThereforeReportClass to avoid any operation.

ReportclassCyclically process the dataset tables [0] to create a multi-page report. CallLoadpageMethod To load enough data (first clear)DatasetTo generate the next report.LoadpageMethods can be rewritten, so you can provide your own logic here. In addition,LoadpageThe method calls the virtual function.OnnextrowYou can override this virtual function for special processing, such as checking data separation and calculating subtotal (you can save it to a table in the dataset. If you do want to override these methods and force paging, Set Report. m_pagefullTrue. To completely stop further output, SetMoredataSetTrue.LoadpageYesDatabindMethod.

Page. RenderMethod To insert HTML to force paging. CallAllcontrols. rendercontrolsMethod to force the display of various controls on the page. Repeat the preceding steps until the data rows in the specified dataset are exhausted. End HTML Tag (Controls [2]. rendercontrol) Called, Database Connection closed,RenderComplete.

Back to Top Summary

This example shows how to use a few lines of code to change the basic example of ASP. Net from an HTML form to a multi-page report. Existing ASP. the vast majority of the net infrastructure is intact-All toolbox controls are available. You can set the rendering properties of these controls through the Properties window, and the real rendering process is still executed by the existing code. However, the output can be used for different purposes.

Back to Top

We would like to thank the following:

David Powell and Scott Beaudreau from Microsoft consulting services helped us start using. NET and guided the team to complete the initial phase of the project.

Mark Wadsworth, Frank degise, and Linda Sutton continue to provide support and encouragement so that we can publish this article.Article.

Tushar Patwardhan and neeraj pathania helped test and fix all the problems. We would also like to thank the team members of Chandra at EDS Corporation.

Back to Top
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.