Setting Parameters from ListBox Selections
In this section you add a button to redisplay the report based on selections from the defaultParameterValuesListListBox control. Within the event method for this button, you call the same method that is called when the page first loads: SetCurrentValuesForParameterField(). But this time, rather than pass in arbitrary values (Paris and Tokyo), you pass in the selected values from the defaultParameterValuesListListBox control.
To create and configure a redisplay Button on the form
- Open the Web or Windows Form.
- From the View menu, click Designer.
- From the Toolbox, drag a Button control to the right of the ListBox control.
- Click on the Button control to select it.
- From the Properties window, do the following:
- Set the ID or Name to "redisplay."
- Set the Text to "Redisplay Report."
To create the click event method for the redisplay Button in a Web project
You are now ready to create a button click event method that checks for selected items in the ListBox control, and passes them to SetCurrentValuesForParameterField() method.
Your code varies slightly for a Web project or a Windows project, so only complete either the Web or Windows procedure below.
- Double-click the Redisplay Report button.
You are taken to the code-behind class where a redisplay\_Click() event method has been automatically generated.
- Above the class signature, add an "Imports" [Visual Basic] or "using" [C#] declaration to the top of the class for the System.Web.UI.WebControls namespace (if this namespace has not already been declared).
``` vb
Imports System.Web.UI.WebControls
```
``` csharp
using System.Web.UI.WebControls;
```
Within the redisplay_Click() event method that has just been auto-generated, declare and instantiate an ArrayList.
Dim myArrayList As ArrayList = New ArrayList()
ArrayList arrayList = new ArrayList();
Create a foreach loop to retrieve each ListItem instance from the Items property of defaultParameterValuesList ListBox.
For Each item As ListItem In defaultParameterValuesList.Items Next
foreach(ListItem item in defaultParameterValuesList.Items) { }
Within the foreach loop, create a nested conditional block that checks whether the Selected property of the current Item instance is set to True.
If item.Selected Then End If
if(item.Selected) { }
Within the conditional block, add the Value property of the Item instance to the ArrayList instance.
myArrayList.Add(item.Value)
arrayList.Add(item.Value);
Outside the conditional block, and outside the foreach loop, rebind the file directory path of the CustomersByCity report to the ReportSource property of the CrystalReportViewer class.
Note
The file directory path that is shown below is for a Visual Studio project. "ProjectName" is replaced by the name of your Web Site. "UserName" is replaced by your logon name.
The default path for a Web Site project:
myCrystalReportViewer.ReportSource = "C:\WebSites\ProjectName\CustomersByCity.rpt"
crystalReportViewer.ReportSource = "C:\\WebSites\\ProjectName\\CustomersByCity.rpt";
Retrieve the ParameterFields instance from the ParameterFieldInfo property of the CrystalReportViewer control.
Dim myParameterFields As ParameterFields = myCrystalReportViewer.ParameterFieldInfo
ParameterFields parameterFields = crystalReportViewer.ParameterFieldInfo;
Pass in the ParameterFields and ArrayList instance to the SetCurrentValuesForParameterField() method.
SetCurrentValuesForParameterField(myParameterFields, myArrayList)
SetCurrentValuesForParameterField(parameterFields, arrayList);
To create the click event method for the redisplay Button in a Windows project
- Double-click the redisplay Button control.
You are taken to the code-behind class where a redisplay\_Click() event method has been automatically generated.
Within the redisplay_Click() event method that has just been auto-generated, declare and instantiate an ArrayList.
Dim myArrayList As ArrayList = New ArrayList()
ArrayList arrayList = new ArrayList();
Create a foreach loop, to retrieve each item (as a string) from the SelectedItems property of defaultParameterValuesList ListBox.
For Each item As String In defaultParameterValuesList.SelectedItems Next
foreach(string item in defaultParameterValuesList.SelectedItems) { }
Within the foreach loop, add item string instance to the ArrayList instance.
myArrayList.Add(item)
arrayList.Add(item);
Outside the foreach loop, rebind the file directory path of the CustomersByCity report to the ReportSource property of the CrystalReportViewer class.
> [!NOTE]
> <P>The file directory path that is shown below is for a Visual Studio project. "ProjectName" is replaced by the name of your Windows project. "UserName" is replaced by your logon name.</P>
The default path for a Windows project:
``` vb
myCrystalReportViewer.ReportSource = "C:\Documents and Settings\UserName\My Documents\Visual Studio\Projects\ProjectName\CustomersByCity.rpt"
```
``` csharp
crystalReportViewer.ReportSource = "C:\\Documents and Settings\\UserName\\My Documents\\Visual Studio\\Projects\\ProjectName\\CustomersByCity.rpt";
```
Retrieve the ParameterFields instance from the ParameterFieldInfo property of the CrystalReportViewer control.
Dim myParameterFields As ParameterFields = myCrystalReportViewer.ParameterFieldInfo
ParameterFields parameterFields = crystalReportViewer.ParameterFieldInfo;
Pass in the ParameterFields and ArrayList instance to the SetCurrentValuesForParameterField() method.
``` vb
SetCurrentValuesForParameterField(myParameterFields, myArrayList)
```
``` csharp
SetCurrentValuesForParameterField(parameterFields, arrayList);
```
To test the population of the defaultParameterValuesList ListBox control
Now that the selected values from the ListBox control have been applied as the current values for the parameter field, you are ready to redisplay the report.
You are now ready to build and run the project, to verify that the parameter field has been reset successfully.
From the Build menu, select Build Solution.
If you have any build errors, go ahead and fix them now.
From the Debug menu, click Start.
In the ListBox control, CTRL-click to select different cities in the list.
Click the Redisplay Report button.
The page reloads and displays the customer records for customers who live in the list of cities that you have selected.
- Return to Visual Studio and click Stop to exit from debug mode.