Visual studio 2008 comes with a crystal report 10
Simple Application in ASP. NET websites
The purpose of this example is to allow friends who are new to the Crystal Report to master their applications. Therefore, the example is simple, concise, and clear, and does not connect to the background database.
Visual studio 2008 create an asp.net website CrystalReportsDemo
Right-click the project --> Add reference:
Add an object class DemoModel. cs
This website does not adopt any development mode, so the class code should be put in the App_Code folder of asp.net,
You can create a folder in the project, or create a folder based on the system prompt when adding a category:
The content of DemoModel. cs is as follows:
Using System; using System. data; using System. configuration; using System. linq; using System. web; using System. web. security; using System. web. UI; using System. web. UI. htmlControls; using System. web. UI. webControls; using System. web. UI. webControls. webParts; using System. xml. linq; /// <summary> /// DemoModel an object // </summary> public class DemoModel {public DemoModel () {} public DemoModel (string id, string name) {this. id = id; this. name = name;} private string id; // encode public string Id {get {return id;} set {id = value;} private string name; // Name public string name {get {return name;} set {Name = value ;}}}
Right-click Project --> Add new item --> Crystal REPORT
After the name is "CrystalReport. rpt" is determined, the following prompt is displayed automatically:
There are three options for creating a new Crystal Report document:
You can select an empty report and right-click the open report --> report expert (same effect ),
By default, the Report Wizard is used. select experts as the standard (usually used) and click OK.
Project Data -->. Net object
The added object DemoModel. cs is of course only a data source.
Note: The above Entity class and the crystal report file can be found in the same assembly. When the website adopts the N-layer mode for development,
The entity class is an assembly and cannot be found here. You can use another data source.
Add DemoModel to the right, as shown in:
Click Next to set the fields to be displayed in the report.
Define the report format here and click Finish.
On the left side of the page, you can see the report header, header, details, end of the report, and footer, which are convenient for designing the report format. In the corresponding area, click
You can right-click it and set it. After the crystal report is applied, you can try it. Here, we only set the detailed information area, from the right, field Resource Manager
Database field. Find the added data source DemoModel. You can view the object attributes and drag the fields to the details area on the left.
Of course, the display style can be defined by yourself. You can try to right-click a lot of parameters involved in this report and choose the field resource manager on the right.
Parameter Field (right-click) -- New:
Click "OK" (add a parameter args_1 to the parameter field) and add a parameter args_2 in the same way as above.
To display the two parameter values passed from the outside in the report, drag the two parameters in the parameter field to the header area of the report:
The id displayed in the header area. The name field is the report header. You can customize the name and right-click --> edit text object (rename ):
Now the report settings are complete. Save and close the crystal report window.
In this case, the project directory
The designed crystal report is displayed in Default. aspx.
Drag a control from the report bar of the toolbox to display the crystal report.
CrystalReportViewer can not be used for any other work,
The Code on the Default. aspx page is as follows:
<% @ Page Language = "C #" AutoEventWireup = "true" CodeFile = "Default. aspx. cs "Inherits =" _ Default "%> <% @ Register Assembly =" CrystalDecisions. web, Version = 10.5.3700.0, Culture = neutral, PublicKeyToken = 692fbea5521e1304 "Namespace =" CrystalDecisions. web "TagPrefix =" CR "%> <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
The content of the Default. aspx. cs post-page class is as follows:
Using System; using System. configuration; using System. data; using System. linq; using System. web; using System. web. security; using System. web. UI; using System. web. UI. htmlControls; using System. web. UI. webControls; using System. web. UI. webControls. webParts; using System. xml. linq; // introduce the namespace // List is used, and there is a generic using System. collections; using System. collections. generic; using CrystalDecisions. shared; using CrystalDecisions. C RystalReports. engine; public partial class _ Default: System. web. UI. page {protected void Page_Load (object sender, EventArgs e) {// create a report document ReportDocument myReport = new ReportDocument (); // obtain the report file physical path string reportPath = Server. mapPath ("~ /CrystalReport. rpt "); // load the report file myReport. load (reportPath ); // @/* required parameters for report display, here we define two parameters according to the parameter */string args_1 = "first parameter"; string args_2 = "second parameter "; // input the defined two external parameters to the report ParameterFields paramFields = new ParameterFields (); ParameterField pargs_1 = new ParameterField (); ParameterField pargs_2 = new ParameterField (); // set pargs_1.ParameterFieldName = "args_1"; pargs_2.ParameterFieldName = "args_2"; ParameterDiscreteValue pdargs_1 = new ParameterDiscreteValue (); parameterDiscreteValue pdargs_2 = new ParameterDiscreteValue (); pdargs_1.Value = args_1; pdargs_2.Value = args_2; pargs_1.CurrentValues.Add (pdargs_1); Round (pdargs_2); paramFields. add (pargs_1); paramFields. add (pargs_2); // bind the parameter set to the report browsing control this. crystalReportViewer1.ParameterFieldInfo = paramFields; // parameter settings are complete //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@//** **************************************** * ********************/* because the database is not connected, therefore, we add a data source for the display effect and use the object in the report. Here we manually add the following objects and load them into a List set, of course, in the future, if you connect to the database, it will also mainly modify the data source. Do not make too many changes */DemoModel dm1 = new DemoModel ("10001", "demo1"); DemoModel dm2 = new DemoModel ("10002 ", "demo2"); DemoModel dm3 = new DemoModel ("10003", "demo3"); DemoModel dm4 = new DemoModel ("10004", "demo4 "); list <DemoModel> dmList = new List <DemoModel> (); dmList. add (dm1); dmList. add (dm2); dmList. add (dm3); dmList. add (dm4 ); // The data source object has been added //******************************* * ********************************* // The new report document set the data source myReport. setDataSource (dmList); // bind the created report document to this. crystalReportViewer1.ReportSource = myReport; this. crystalReportViewer1.DataBind ();}}
Object-based crystal report Data Source
After the parameter development method is added, the following results can be obtained after compilation and running: