Using the Crystal Report Viewer for Web Forms

For report files that live externally to your application (for instance, as a standalone report file, created with either this or a previous version of Crystal Reports), there is not much to creating a simple preview form for your report. We are going to walk through that process in the following section.

Earlier we created a new project called web_viewer_basic and within that project there should be a default Web Form (WebForm1.aspx) that was created when you created the project. To start, we need to drag or draw the Crystal Report Viewer onto our Web Form, as shown in Figure 5-2.

From that point, we need to set the ReportSource property to let the viewer know where to get the report. To access this property, locate the Properties window for the Crystal Report Viewer and open the (DataBindings) property, by clicking the ellipse at the side, to show the dialog that appears in Figure 5-3.

Tablelayoutpanel
Figure 5-3

Click the ReportSource property and click the radio button to select Custom Binding Expression. In this example, we are going to assume that you have unzipped the download files for this chapter to your hard drive in a folder called c:\Crystal.NET2 003\Chapter05. Included in these files is a Sales Graph Report (sales_graph.rpt) that we will be using in this walkthrough.

Once you have entered the name and path for your report, click OK to accept your changes and return to the form we were working with. The report will now be displayed in a Preview mode in the form designer, as shown in Figure 5-4.

If we were working with a Windows Form, this would be all that is required to actually preview a report. Because Web Forms work a little differently, there is an extra step involved before we can run our application and preview our report.

Figure 5-4

Double-click anywhere on your form to open the code view for the form and locate the section of code marked Web Form Designer generated code. Expand this section to find the Page_lnit function and add a line of code immediately after the lnitializeComponent() call to bind your report to the viewer when the page is initialized:

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init 'CODEGEN: This method call is required by the Web Form Designer 'Do not modify it using the code editor. InitializeComponent() CrystalReportViewer1.DataBind() End Sub

Whenever you run your application and preview the form, your report will be displayed in the Crystal Report Viewer, as shown in Figure 5-5.

Figure 5-5

The viewer interacts with the Crystal Reports Print Engine, runs the report, and displays the results. From your report preview, you can drill-down into the details or search for a value, without having to do any additional coding.

If you have only one or two reports that you want to integrate into a view-only application and you don't need to customize any features at run time, this may be all you need. But for applications that required a more sophisticated integration with Crystal Reports .NET, you probably need to look a bit further.

In the following sections, we are going to walk through adding a report to your application, binding the report to the Crystal Report Viewer, and customizing the viewer.

0 0

Post a comment