配置网站的 Session 持久性

在本节中,您将学习如何为按钮单击事件配置 Session 持久性。

如果在发生按钮单击事件时重新加载 Web 页,对 CrystalReportViewer 对象模型所作的更改将会丢失。

演示网站缺少持久性的情形

  1. 从“生成”菜单中选择“生成解决方案”。

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

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

  4. 从 selectBackColorDropDownList 中,选择“Blue”。

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

    页面会重新加载,在蓝色背景上显示没有工具栏的报表。

  6. 对于边框宽度,键入“10”。

  7. 对于边框样式,选择“Double”。

  8. 对于边框颜色,选择“SteelBlue”。

  9. 单击“绘制边框”。

页面会重新加载,并在 Crystal 报表周围显示边框,而且背景颜色不再是蓝色。
  1. 在“pageNumberTextBox” 中输入“3”,然后单击 “转到页”。

    页面会重新加载,并显示报表的第 3 页,而且不会再看到报表周围的边框了。

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

向 drawBorder_Click() 事件处理程序添加 Session 赋值代码

必须向应用程序添加持久性代码,这样在重新加载 Web 页面时 CrystalReportViewer 对象模型中所做的更改才能保持。

首先,要向 drawBorder_Click() 事件处理程序添加针对边框值的持久性代码(这些值在此处理程序中第一次被赋值)。随后,在 ConfigureCrystalReports() 方法中,获取存储在 Session 中的值,并将这些值赋给 CrystalReportViewer 类的各自属性。

  • 在 drawBorder_Click() 事件处理程序中,在现有代码之后为 BackColor、BorderColor、BorderStyle 和 BorderWidth 添加四个 Session 赋值。
``` vb
Session("myBorderColor") =
myCrystalReportViewer.BorderColor.ToString()
Session("myBorderStyle") = myCrystalReportViewer.BorderStyle
Session("myBorderWidth") = myCrystalReportViewer.BorderWidth
```

``` csharp
Session["borderColor"] =
crystalReportViewer.BorderColor.ToString();
Session["borderStyle"] = crystalReportViewer.BorderStyle;
Session["borderWidth"] = crystalReportViewer.BorderWidth;
```

向 ConfigureCrystalReports() 方法添加 Session 检索代码

现在,可以在 ConfigureCrystalReports() 方法内从 Session 中获取这些值。

  1. 在 ConfigureCrystalReports() 方法的底部,创建一个检查 BackColor Session 变量是否不为空的 If 块。
如果它不为空,则在 If 块内,从 Session 中获取 BackColor 属性并将其转换为字符串。将字符串传递给 Color 类的 FromName() 方法,并将 Color 实例赋给 CrystalReportViewer 实例的 BackColor 属性。

``` vb
If Not IsNothing(Session("myBackColor")) Then
myCrystalReportViewer.BackColor = Color.FromName(CType(Session("myBackColor"), String))
End If
```

``` csharp
if (Session["backColor"] != null)
{
crystalReportViewer.BackColor = Color.FromName((string)Session["backColor"]);
}
```
  1. 创建第二个 If 块,该块检查 BorderColorSession 变量是否不为空。如果它不为空,则在 If 块内,从 Session 中获取 BorderColor 属性并将其转换为字符串。将字符串传递给 Color 类的 FromName() 方法,并将 Color 实例赋给 CrystalReportViewer 实例的 BorderColor 属性。
``` vb
If Not IsNothing(Session("myBorderColor")) Then
myCrystalReportViewer.BorderColor = Color.FromName(CType(Session("myBorderColor"), String))
End If
```

``` csharp
if (Session["borderColor"] != null)
{
crystalReportViewer.BorderColor = Color.FromName((string)Session["borderColor"]);
}
```
  1. 创建第三个 If 块,该块检查 BorderStyleSession 变量是否不为空。如果它不为空,则在 If 块内,从 Session 中获取 BorderStyle 属性并将其转换为 BorderStyle。

    myCrystalReportViewer.BorderStyle = CType(Session("myBorderStyle"), BorderStyle)
    
    if (Session["borderStyle"] != null)
    {
    crystalReportViewer.BorderStyle = (BorderStyle)Session["borderStyle"];
    }
    
  2. 创建第四个 If 块,该块检查 BorderWidth Session 变量是否不为空。如果它不为空,则在 If 块内,从 Session 中获取 BorderWidth 属性并将其转换为整数。

    myCrystalReportViewer.BorderWidth = Convert.ToInt32(Session("myBorderWidth"))
    
    if (Session["borderStyle"] != null)
    {
    crystalReportViewer.BorderWidth = Convert.ToInt32(Session["borderStyle"]);
    }
    

为绘制边框 Button 控件编写代码

在下一个过程中,您要向重新显示按钮单击事件处理程序添加 Session 持久性。

  1. 在 redisplay_Click() 事件处理程序中,将从 selectBackColor DropDownList 中选定的项赋给 Session。

    Session("myBackColor") = selectBackColor.SelectedItem.Text
    
    Session["backColor"] = selectBackColor.SelectedItem.Text;
    
  2. 在 drawBorder_Click() 事件处理程序中,将 borderWidth TextBox 的 Text 属性赋给 Session。

    Session("myBorderWidth") = borderWidth.Text
    
    Session["borderWidth"] = borderWidth.Text;
    
  3. 将从 selectBorderStyleDropDownList 选定的索引赋给 Session。

    Session("myBorderStyle") = selectBorderStyle.SelectedIndex
    
    Session["borderStyle"] = selectBorderStyle.SelectedIndex;
    
  4. 将从 selectBorderColorDropDownList 选定的项赋给 Session。

    Session("myBorderColor") = selectBorderColor.SelectedItem.Text
    
    Session["borderColor"] = selectBorderColor.SelectedItem.Text;
    

现在,即可生成并运行项目,以验证在整个按钮单击事件中将保持对报表所做的更改。