Rendering Multiple Reports
This tutorial describes a simple application that allows the user to select the report to view.
You can start a new ASP.NET application and repeat the steps in the WebReports for ASP.NET Quick Start or use that project as a starting point. Complete the following steps:
1. From the Toolbox, double-click the DropDownList control to add it to the Web form. Note that this list will contain the reports in the report definition file.
2. To update the report when a new item is selected, set the DropDownList.AutoPostBack property to True in the Properties window.
3. Populate the report list by double-clicking the Web form and adding the following code to the Page_Load event:
' Populate report list (only once, the items become ViewState after this)
If Not IsPostBack Then
Dim fileName As String = C1WebReport1.ReportSource.FileName
Dim names As String() = C1WebReport1.Report.GetReportInfo(fileName)
Dim name As String
For Each name In names
DropDownList1.Items.Add(name)
Next name
' Select the first report on the list
DropDownList1_SelectedIndexChanged(Me, EventArgs.Empty)
End If
• C#
// Populate report list (only once, the items become ViewState after this)
if (!IsPostBack)
{
string fileName = C1WebReport1.ReportSource.FileName;
string[] names = C1WebReport1.Report.GetReportInfo(fileName);
foreach (string name in names)
DropDownList1.Items.Add(name);
// Select the first report on the list
DropDownList1_SelectedIndexChanged(this, EventArgs.Empty);
}
The code starts by checking the IsPostBack property on the page. If the property is set to False, the page is being loaded for the first time, and you need to populate the list box. After that, the items become part of the page's ViewState and do not have to be loaded again.
To populate the list, the code uses the contained C1Report component and its GetReportInfo method, which returns list with the names of the reports in a report definition file.
Note that this code assumes that the FileName property has already been set at design time. This was described in step 3 of the WebReports for ASP.NET Quick Start.
4. To handle user selections, double-click the DropDownList control to add an event handler for the SelectedIndexChanged event. The Code Editor will open with the insertion point placed within the event handler.
5. Insert the following code:
C1WebReport1.ReportSource.ReportName = DropDownList1.SelectedItem.Text
• C#
C1WebReport1.ReportSource.ReportName = DropDownList1.SelectedItem.Text;
This will set the ReportName property to the selected report. The control will automatically load the selected report and render it.
Run the project and observe the following:
In a few seconds the browser will appear. Note that the drop-down list contains a list of reports available in the report definition file.
Select any report to display it in the C1WebReport control below the list.
Notice how the reports take a second or two to render the first time you select them (depending on how fast your machine is), and load almost instantly after that. This is because the reports are cached on the server. If you select a report that has been rendered recently, the control reuses it automatically.
|