Binding Reports to the Web Viewer
After adding a viewer to the web page and setting its properties, the report needs to be bound to the viewer. This can be done with any of the three options listed earlier in the chapter: using the ReportDocument component, using Untyped reports, and using Strongly-Typed reports.
Using the ReportDocument Component with the Viewer
The ReportDocument component makes it easy to specify which report the viewer is supposed to display. Add a ReportDocument component to the web page by double-clicking on the ReportDocument component. The ReportDocument component is listed in the Components section of the Toolbox. When you add a ReportDocument component to your form, it automatically displays the Choose a ReportDocument dialog box. This lets you select which report will be displayed. The dropdown control lists all the reports that are part of your project. For this example, it will only show the Employee List report.
Figure 3-4. The ReportDocument dialog box.
Select the Employee List report and click the OK button. This adds the ReportDocument component to your form.
You have to associate the ReportDocument component with the viewer control so that the viewer knows which report to display. Look at the viewer’s properties and at the very top is the DataBindings property. Click on it and then click on the ellipses button to open the DataBindings dialog box.
Figure 3-5. The DataBindings dialog box.
Click on the ReportSource property listed on the left. Then expand the Page item in the Simple Binding window. It shows you the reports that were added to the page using ReportDocument component.
Select the report name and click the OK button. Once the dialog box closes, a preview of the report is immediately displayed. Unlike Windows development, the viewer component shows you what the report looks like while you are in design mode.
When the application runs, you have to tell the viewer to use the DataBindings property. This is done by calling the DataBind() method. Put this code in the Page_Load() event.
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
CrystalReportViewer1.DataBind()
End Sub
Run the web application and you’ll see the Employee List report automatically previewed when the page opens.