Create multi-page custom report using ASP.NET

xiaoxiao2021-03-06  30

Release Date: 4/1/2004

| Update Date: 4/1/2004

Chandra Kamalakantha

EDS CORPORATION

Marius RoChon

Microsoft Corporation

September 2002

Applicable to:

Microsoft ASP.NET

Microsoft Visual Studio .NET

Summary: Telling a method of generating a simple and complex multi-page HTML report using an existing ASP.NET infrastructure (designer, data binding, and other runtime features). (7 page print pages)

Download ReportTool.exe.

This page

Introduction Our method uses data binding to create a report to run time-fax thank the author

Introduction

This article describes a method of generating a simple and complex report using existing ASP.NET Infrastructure - Designers, Data Binding, and Other Runtime. The report consists of HTML containing multi-page 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 to make the ASP.NET produce repeat sequences of the same HTML output until all report data is exhausted. In order to achieve this, we extend the functionality of the System.Web.ui.page class. Specifically, we rewrite the render method and add additional other design time. With this new class, developers can use standard Microsoft Visual Studio .NET Form Designers to set the layout of the report page, set their properties, and the properties of the various controls included, and execute the item to generate binding to pass. An HTTP response stream having the same layout in each continuous component of the data.

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

We extend the functionality of the new class to allow multiple input data streams, using multiple optional page layouts in the same report (for example, first page, report total page, bright page, etc.), and can page by page Browse the entire report (without repeated access to the server). All related code is included in the ReportClass project. A sample report definition using this framework is included in the MSDNArticleTest project.

Back to top

our way

ASP.NET supports an HTML Form to rendering a page (HTML Form). Therefore, it can be used to respond according to the request of each page of the report, thereby rendering the page. However, this requires user intervention to generate the entire report and needs to maintain a cursor state between different access to the server. If it is integrally generated, this may be a relatively expensive need.

However, ASP.NET also provides an expandable framework for design and runtime, which makes we change its default behavior. In this method, we have used a new class to override the System.Web.ui.page class (by default, the ASP.NET form is derived from this class) default form processing.

The basic operation of the REPORT class is reflected in its render method, and the method is rewritten (and replaced) the render method in System.Web.ui.page:

First, skip the HTML generation process associated with the Form mark - this is unnecessary and actually interferes with the reuse of other control IDs. Load a dataset (with the page size of DataGrid) with enough data. Next, call DATABIND to associate data with all controls on the page. The standard render method of each control is rendered; first load the data set, repeat this operation until the data is not loaded. Finally, call the end tagged render method. Back to top

Create a report using data binding

We will have the sample report (described below) will provide the following characteristics of the report:

A multi-page report to view all author (step 1 to 13)

When the author status changes, pagination (step 14)

According to the total number of postal coded tracks (step 15)

List all overwritten functions

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

Create a report using data binding

1. Create a new solution and name it "CustomReporting".

2. Create a new C # item of a class library type, name it "ReportClass".

3. Delete the class1.cs module.

4. Download the included ReportPage.cs module to your computer.

5. Click the ReportClass project, click Add Existing Item, and then add ReportPage.cs to the ReportClass project.

6. Add reference to System.Data and System.Web in the ReportClass project.

7. Generate class libraries to prepare for data binding to create a report.

8. Add a new ASP.NET Web project to the CustomReporting solution and name it "ReportWritertest".

9. Add a reference to ReportClass to ReportWritrtest. (Click References, on the Projects tab, select the ReportClass project.) Contains ReportClass.dll (displayed in the SELECTED Components section).

10. Edit WebForm1.aspx by adding ReportClass.

11.

Public class Webform1: System.Web.ui.page

change to

Public Class TestReport: ReportClass.ReportPage

This will force the web page to inherit from ReportClass.ReportPage, which tests the Web page to use the properties and methods disclosed by the ReportPage class (part of the ReportClass class library).

12. Generate the ReportWritrtest project.

13. Edit WebForm1.aspx in the designer and design the report layout. In this example, we will generate a report according to the AUTHORS table (part of the PUBS database reside in Microsoft SQL Server®).

Edit WebForm1.aspx and design report layout

a. Add a HTML form that includes two rows.

b. Edit HTML and delete the style property of the table.

c. Set cellspacing, cellpadding, and border to 0. •

In the first line of the table, add two tag controls. The first tag is used for the PAGE tag, and the second tag will display the page number; therefore it will be named "lblpage" and the text should be set to space.

e. In the second line of the table, add two tag controls. The first tag control will be used for label tag status. The second tag control will be named "lblstate", which will be set to Empty Spaces.

f. On the third line, delete three columns in the four columns, only one column remains. Set the width of the column to the same width.

g. In the HTML form, drag a data binding grid control to a third line.

H. Edit the data binding grid column collection and set up four columns; used for the author's last name, name, address, and city. Use Bound Column and the total number of pages of postal coding statistics. Please note: When running, cancel the Create Column Automatically.

i. On the fourth line, add another two labels. The first tag text will be set to Total, and the text of the second tag is set to EMPTY SPACES and will be named "lbltotal".

j. Embed the entire table into a panel and specify the ID to Page1 to the panel. Make sure the panel is set to run on the server.

k. Drag a non-calorization data set to the form.

l. Navigate to the Properties tab and select Webform1.aspx page. Set the DataSet property to the data set on the page. Set the Data-Bound Grid to the data binding grid on the page.

m. Fill this dataset in the Page_Load event of the web page.

n. Contains the following code to fill the data set:

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 (SCONNNESTRING);

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 / components according to the column:

Base.TrackColumnBreaks ("One", "State"); this will provide an override function and exposes the rotumn / page break.

Protected Override Void ProcessPageBreak ()

{

LBLSTATE.TEXT = CURRENTDATAROW ["state"]. TOSTRING ();

}

15. Set and track the total:

Base.tracktotals ("Zip", ReportClass.totalscopeEnum.page;

Base.Tracktotals ("Zip", ReportClass.Totalscopeenum.Report;

16. Other rewritable functions include: ProcessPageBreak, PostProcessPageBreak, ProcessGroupBreak, PostProcessGroupBreak, ProcessReport, and ProcessCurrentrow. It is recommended not to override ProcessCurrentrow. Developers can override these functions to reset or print content depending on the specific circumstances.

17. To add a navigation bar in the report, include Pagenav.js to the project and include it to each report page. Include ViewReport.aspx, ReportDetail.htm, and ReportHeader.htm are included in the project. ShowReport.aspx contains an example to show how to call ViewReport.aspx for page navigation. The unique action completed by this navigation function is to move between individual links (eg, http: //localhost/msdntest/webform1.aspx#page [1-n]).

Back to top

Runtime

What happens when you do this report? The actual operation of the Report class is in its render method, which rewrites the same method in the standard PAGE class.

Starting the HTML tag is rendered by calling the Controls [0] .Render method. At some point, Control.Render will call page.verifyrenderingInServerForm. The default implementation method of this function will trigger an exception; therefore, rewriting this function in the Report class is to do not perform any operations.

The ReportClass loop processing dataset tables [0] to create a multi-page report. Call the LoadPage method to load enough data (first cleared) into the DataSet to generate the next page. The LoadPage method is rewritten, so you can provide your own logic here. In addition, the LoadPage method calls the virtual function OnNexTrow, you can rewrite this virtual function to make some special processing, for example, detect data separation, calculating a small meter (you can save it in a table of the dataset) and so on. If you do to rewrite these methods, and want to force a paging, you should set the report.m_pagefull to true. To completely stop further output, set moredata to True. LoadPage will call the DataBind method.

Page.Render method is inserted into HTML to force a paging. Call allControls.RenderControls methods to enforce each control on the page. The above steps are repeated until the data line is exhausted in the specified data set. End HTML tag (Controls [2] .RenderControl) is called, the database connection is turned off, and Render is completed. Back to top

summary

This example illustrates how to use relatively few lines of code to change the basic example of ASP.NET from rendering HTML form to generate multiple page reports. Most of the existing ASP.NET infrastructure is not moving - all toolbox controls are available, and the rendering properties of these controls can be set through the Properties window, and the real rendering process is still executed by existing code. However, the generated output can be used as a significant different use.

Back to top

Thank you

Here, we want to thank the following people:

The David Powell and Scott Beaudreau of the Microsoft Consulting Services help us start using the .NET and instructing the team to complete the initial stage of this project.

Mark Wadsworth, Frank Degise and Linda Sutton continue to support and encourage us to publish this article.

Tushar Patwardhan and Neeraj Pathania help test and fix all questions, we also would also thank Chandra in group colleagues in EDS Corporation.

Back to top

About author

Chandra Kamalakantha has worked in EDS Corporation, which is a leading global information technology (IT) service company. If you have any questions and comments to Chandra, send an email to Chandra.kamalakantha@eds.com.

Marius RoChon is a Technical Evangelist for Microsoft.

转载请注明原文地址:https://www.9cbs.com/read-40373.html

New Post(0)