为网站配置 ListBox 控件

本节说明如何为网站配置 ListBox 控件。如果所构建的是 Windows 项目,请参见“为 Windows 项目配置 ListBox 控件”

现在,可以为 Button 控件创建单击事件处理程序,然后将代码添加到此事件处理程序中。此事件处理程序根据用户在 ListBox 控件中所做的选择,为 CrystalReportViewer 类的工具栏属性设置不同的布尔值。

在创建此事件处理程序之前,需要创建两个枚举: CeWebCRVReportOptions 和 CeWebCRVToolbarOptions。

这两个枚举提供可选择的报表元素和工具栏元素的列表。

创建 CeWebCRVReportOptions 枚举

  1. 在“解决方案资源管理器”中,右击网站名,指向“添加”,然后单击“添加新项”。

  2. 在“添加新项”对话框中,从“模板”视图选择“类”。

  3. 在“名称”字段中,键入“CeWebCRVReportOptions”,然后单击“添加”。

<table>
<colgroup>
<col style="width: 100%" />
</colgroup>
<thead>
<tr class="header">
<th><img src="images/8yfdxzdx.alert_note(zh-cn,VS.90).gif" alt="Note" class="note" />注意</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><p>可能会要求将此类放置在 App_Code 目录中。单击“是”按钮。</p></td>
</tr>
</tbody>
</table>
  1. 在类签名中,将单词 class 改为“enum”,以将该类转换为枚举。

    Note注意

    在 Visual Basic 中,要记住把类的开始和结束签名都更改为 enum。

  2. 因为枚举没有构造函数,所以要删除代码的 C# 版本中提供的默认构造函数方法。

  3. 在枚举内部,输入值:

    Toolbar
    Group_Tree
    Main_Page
    Enable_Separate_Pages
    
    Toolbar,
    Group_Tree,
    Main_Page,
    Enable_Separate_Pages
    
  4. 从“文件”菜单中,单击“全部保存”。

创建 CeWebCRVToolbarOptions 枚举

  1. 在“解决方案资源管理器”中,右击网站名,指向“添加”,然后单击“添加新项”。

  2. 在“添加新项”对话框中,从“模板”视图选择“类”。

  3. 在“名称”字段中,键入 CeWebCRVToolbarOptions,然后单击“添加”。

<table>
<colgroup>
<col style="width: 100%" />
</colgroup>
<thead>
<tr class="header">
<th><img src="images/8yfdxzdx.alert_note(zh-cn,VS.90).gif" alt="Note" class="note" />注意</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><p>可能会要求将此类放置在 App_Code 目录中。单击“是”按钮。</p></td>
</tr>
</tbody>
</table>
  1. 在类签名中,将单词 class 改为“enum”,以将该类转换为枚举。
<table>
<colgroup>
<col style="width: 100%" />
</colgroup>
<thead>
<tr class="header">
<th><img src="images/8yfdxzdx.alert_note(zh-cn,VS.90).gif" alt="Note" class="note" />注意</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><p>在 Visual Basic 中,要记住把类的开始和结束签名都更改为 enum。</p></td>
</tr>
</tbody>
</table>
  1. 因为枚举没有构造函数,所以要删除代码的 C# 版本中提供的默认构造函数方法。

  2. 在枚举内部,输入值:

    Group_Tree_Button
    Export_Button
    Print_Button
    View_List_Button
    Drill_Up_Button
    Page_Navigation_Button
    Go_to_Page_Button
    Search_Button
    Zoom_Button
    Crystal_Logo
    
    Group_Tree_Button,
    Export_Button,
    Print_Button,
    View_List_Button,
    Drill_Up_Button,
    Page_Navigation_Button,
    Go_to_Page_Button,
    Search_Button,
    Zoom_Button,
    Crystal_Logo
    
  3. 从“文件”菜单中,单击“全部保存”。

从枚举填充 ListBox 控件

现在将使用枚举值对 ListBox 控件进行填充,这些值代表可用于 CrystalReportViewer 工具栏的属性。

  1. 打开 Web 窗体。

  2. 从“视图”菜单中,单击“代码”。

  3. 在 ConfigureCrystalReports() 方法中,添加 Not IsPostBack 条件块。

<table>
<colgroup>
<col style="width: 100%" />
</colgroup>
<thead>
<tr class="header">
<th><img src="images/8yfdxzdx.alert_note(zh-cn,VS.90).gif" alt="Note" class="note" />注意</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><p>本教程开始时,在ConfigureCrystalReports()过程中创建了 <a href="ms227453(v=vs.90).md">“项目设置”</a> 方法。要正确完成本教程,必须首先执行<a href="ms227453(v=vs.90).md">“项目设置”</a>。</p></td>
</tr>
</tbody>
</table>

``` vb
If Not IsPostBack Then

End If
```

``` csharp
if (!IsPostBack)
{
}
```
  1. 在该条件块中,把 listCRVReportListBox 控件的 DataSource 属性设置为 CeWebCRVReportOptions 枚举的值。

    listCRVReport.DataSource = System.Enum.GetValues(GetType(CeWebCRVReportOptions))
    
    listCRVReport.DataSource = System.Enum.GetValues(typeof(CeWebCRVReportOptions));
    
  2. 调用 listCRVReportListBox 控件的 DataBind() 方法,将值绑定到控件。

    listCRVReport.DataBind()
    
    listCRVReport.DataBind();
    
  3. 接着,将 listCRVToolbarListBox 控件的 DataSource 属性设置为 CeWebCRVToolbarOptions 枚举的值。

    listCRVToolbar.DataSource = System.Enum.GetValues(GetType(CeWebCRVToolbarOptions))
    
    listCRVToolbar.DataSource = System.Enum.GetValues(typeof(CeWebCRVToolbarOptions));
    
  4. 现在调用 listCRVToolbarListBox 控件的 DataBind() 方法,将值绑定到控件。

    listCRVToolbar.DataBind()
    
    listCRVToolbar.DataBind();
    
  5. 在 Not IsPostBack 条件块之外,把 Chart.rpt 文件绑定到 CrystalReportViewer 控件的 ReportSource 属性。

有关示例报表的信息,请参见[“示例报表目录”](ms225622\(v=vs.90\).md)。

``` vb
myCrystalReportViewer.ReportSource = "C:\Program Files\Microsoft Visual Studio 9.0/Crystal Reports\Samples\chs\Reports\Feature Examples\Chart.rpt"
```

``` csharp
crystalReportViewer.ReportSource = "C:\\Program Files\\Microsoft Visual Studio 9.0\\Crystal Reports\\Samples\\chs\\Reports\\Feature Examples\\Chart.rpt";
```

为网站的重新显示 Button 控件编写代码

现在您可以向 Button 控件的单击事件添加代码。单击方法必须为 CrystalReportViewer 类的报表和工具栏元素设置布尔值。如果选择了某个元素,则布尔值设置为 True,并且将显示该报表或工具栏元素。如果属性未被选定,则布尔值设置为 False,并且该报表或工具栏元素将不会显示。

  1. 打开 Web 窗体。

  2. 从“视图”菜单中,单击“设计器”。

  3. 双击重新显示 Button 控件。

    即会出现报表的代码隐藏类,表示已经自动生成 redisplay_Click() 事件方法。

  4. 在 redisplay_Click() 事件方法中,调用 listCRVReport 和 listCRVToolbarListBox 控件中每一项的 Selected 属性。

Selected 属性返回布尔值以设置 CrystalReportViewer 工具栏属性。

<table>
<colgroup>
<col style="width: 100%" />
</colgroup>
<thead>
<tr class="header">
<th><img src="images/8yfdxzdx.alert_note(zh-cn,VS.90).gif" alt="Note" class="note" />注意</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><p>CrystalReportViewer 报表和工具栏元素被设置为它们在 CeWebCRVReportOptions 和 CeWebCRVToolbarOptions 枚举中的相应值。来自枚举类的值返回一个字符串,您需要将其转换为整数。</p></td>
</tr>
</tbody>
</table>

``` vb
myCrystalReportViewer.HasToggleGroupTreeButton = listCRVToolbar.Items(Convert.ToInt32(CeWebCRVToolbarOptions.Group_Tree_Button)).Selected
myCrystalReportViewer.HasExportButton = listCRVToolbar.Items(Convert.ToInt32(CeWebCRVToolbarOptions.Export_Button)).Selected
myCrystalReportViewer.HasPrintButton = listCRVToolbar.Items(Convert.ToInt32(CeWebCRVToolbarOptions.Print_Button)).Selected
myCrystalReportViewer.HasViewList = listCRVToolbar.Items(Convert.ToInt32(CeWebCRVToolbarOptions.View_List_Button)).Selected
myCrystalReportViewer.HasDrillUpButton = listCRVToolbar.Items(Convert.ToInt32(CeWebCRVToolbarOptions.Drill_Up_Button)).Selected
myCrystalReportViewer.HasPageNavigationButtons = listCRVToolbar.Items(Convert.ToInt32(CeWebCRVToolbarOptions.Page_Navigation_Button)).Selected
myCrystalReportViewer.HasGotoPageButton = listCRVToolbar.Items(Convert.ToInt32(CeWebCRVToolbarOptions.Go_to_Page_Button)).Selected
myCrystalReportViewer.HasSearchButton = listCRVToolbar.Items(Convert.ToInt32(CeWebCRVToolbarOptions.Search_Button)).Selected
myCrystalReportViewer.HasZoomFactorList = listCRVToolbar.Items(Convert.ToInt32(CeWebCRVToolbarOptions.Zoom_Button)).Selected
myCrystalReportViewer.HasCrystalLogo = listCRVToolbar.Items(Convert.ToInt32(CeWebCRVToolbarOptions.Crystal_Logo)).Selected
myCrystalReportViewer.DisplayToolbar = listCRVReport.Items(Convert.ToInt32(CeWebCRVReportOptions.Toolbar)).Selected
myCrystalReportViewer.DisplayGroupTree = listCRVReport.Items(Convert.ToInt32(CeWebCRVReportOptions.Group_Tree)).Selected
myCrystalReportViewer.DisplayPage = listCRVReport.Items(Convert.ToInt32(CeWebCRVReportOptions.Main_Page)).Selected
myCrystalReportViewer.SeparatePages = listCRVReport.Items(Convert.ToInt32(CeWebCRVReportOptions.Enable_Separate_Pages)).Selected
```

``` csharp
crystalReportViewer.HasToggleGroupTreeButton = listCRVToolbar.Items[Convert.ToInt32(CeWebCRVToolbarOptions.Group_Tree_Button)].Selected;
crystalReportViewer.HasExportButton = listCRVToolbar.Items[Convert.ToInt32(CeWebCRVToolbarOptions.Export_Button)].Selected;
crystalReportViewer.HasPrintButton = listCRVToolbar.Items[Convert.ToInt32(CeWebCRVToolbarOptions.Print_Button)].Selected;
crystalReportViewer.HasViewList = listCRVToolbar.Items[Convert.ToInt32(CeWebCRVToolbarOptions.View_List_Button)].Selected;
crystalReportViewer.HasDrillUpButton = listCRVToolbar.Items[Convert.ToInt32(CeWebCRVToolbarOptions.Drill_Up_Button)].Selected;
crystalReportViewer.HasPageNavigationButtons = listCRVToolbar.Items[Convert.ToInt32(CeWebCRVToolbarOptions.Page_Navigation_Button)].Selected;
crystalReportViewer.HasGotoPageButton = listCRVToolbar.Items[Convert.ToInt32(CeWebCRVToolbarOptions.Go_to_Page_Button)].Selected;
crystalReportViewer.HasSearchButton = listCRVToolbar.Items[Convert.ToInt32(CeWebCRVToolbarOptions.Search_Button)].Selected;
crystalReportViewer.HasZoomFactorList = listCRVToolbar.Items[Convert.ToInt32(CeWebCRVToolbarOptions.Zoom_Button)].Selected;
crystalReportViewer.HasCrystalLogo = listCRVToolbar.Items[Convert.ToInt32(CeWebCRVToolbarOptions.Crystal_Logo)].Selected;
crystalReportViewer.DisplayToolbar = listCRVReport.Items[Convert.ToInt32(CeWebCRVReportOptions.Toolbar)].Selected;
crystalReportViewer.DisplayGroupTree = listCRVReport.Items[Convert.ToInt32(CeWebCRVReportOptions.Group_Tree)].Selected;
crystalReportViewer.DisplayPage = listCRVReport.Items[Convert.ToInt32(CeWebCRVReportOptions.Main_Page)].Selected;
crystalReportViewer.SeparatePages = listCRVReport.Items[Convert.ToInt32(CeWebCRVReportOptions.Enable_Separate_Pages)].Selected;
```

测试重新显示 Button 控件

现在即可生成并运行项目,以自定义 CrystalReportViewer 工具栏。

  1. 从“生成”菜单中,单击“生成解决方案”。

  2. 如果生成过程中出错,请立即纠正。

  3. 从“调试”菜单中,单击“开始”。

listCRVReport 和 listCRVToolbarListBox 控件将显示 CrystalReportViewer 报表和工具栏选项的完整列表。
  1. 在工具栏选项列表框中,选择“Page_Navigation_Button”、“Print_Button”和“Export_Button”。

  2. 在报表选项列表框中,选择“Toolbar”、“Group_Tree”和“Main_Page”。

  3. 单击“重新显示报表”。

    页面将重新加载,显示带有一个可见的工具栏、组树和主页面的 CrystalReportViewer 控件。在工具栏中,只能看见“页面导航”、“打印”和“导出”按钮。

  4. 返回到 Visual Studio,然后单击“停止”从调试模式中退出。