Share via


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

  1. Open the Web or Windows Form.
  2. From the View menu, click Designer.
  3. From the Toolbox, drag a Button control to the right of the ListBox control.
  4. Click on the Button control to select it.
  5. From the Properties window, do the following:
  6. Set the ID or Name to "redisplay."
  7. 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.

  1. Double-click the Redisplay Report button.
You are taken to the code-behind class where a redisplay\_Click() event method has been automatically generated.
  1. 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;
```
  1. 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();
    
  2. 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)
    {
    }
    
  3. 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)
    {
    }
    
  4. Within the conditional block, add the Value property of the Item instance to the ArrayList instance.

    myArrayList.Add(item.Value)
    
    arrayList.Add(item.Value);
    
  5. 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";
    
  6. Retrieve the ParameterFields instance from the ParameterFieldInfo property of the CrystalReportViewer control.

    Dim myParameterFields As ParameterFields =
    myCrystalReportViewer.ParameterFieldInfo
    
    ParameterFields parameterFields =
    crystalReportViewer.ParameterFieldInfo;
    
  7. 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

  1. Double-click the redisplay Button control.
You are taken to the code-behind class where a redisplay\_Click() event method has been automatically generated.
  1. 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();
    
  2. 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)
    {
    }
    
  3. Within the foreach loop, add item string instance to the ArrayList instance.

    myArrayList.Add(item)
    
    arrayList.Add(item);
    
  4. 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";
```
  1. Retrieve the ParameterFields instance from the ParameterFieldInfo property of the CrystalReportViewer control.

    Dim myParameterFields As ParameterFields =
    myCrystalReportViewer.ParameterFieldInfo
    
    ParameterFields parameterFields =
    crystalReportViewer.ParameterFieldInfo;
    
  2. 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.

  1. From the Build menu, select Build Solution.

  2. If you have any build errors, go ahead and fix them now.

  3. From the Debug menu, click Start.

  4. In the ListBox control, CTRL-click to select different cities in the list.

  5. 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.
  1. Return to Visual Studio and click Stop to exit from debug mode.