Export (0) Print
Expand All

Configuring Session Persistence for a Web Site

Visual Studio 2005

In this section, you learn how to configure Session persistence for button click events.

When a Web page is reloaded on a button click event, the changes that are made to the CrystalReportViewer object model are lost.

To demonstrate lack of persistence for a Web Site

  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. From the selectBackColor DropDownList, select "Blue."
  5. Click Redisplay Report.

    The page reloads to display the report with no toolbar, on a blue background.

  6. For the border width, type "10".
  7. For the border style, select "Double".
  8. For the border color, select "SteelBlue".
  9. Click Draw Border.

    The page reloads to display a border around the Crystal report, and the background color is no longer blue.

  10. Enter "3" in the pageNumber TextBox, and then click Go to Page.

    The page reloads to display page 3 of the report, and the border around the report is no longer visible.

  11. Return to Visual Studio 2005 and click Stop to exit from debug mode.

You must add persistence code to your application, so that changes that are made within the CrystalReportViewer object model are persisted when Web pages are reloaded.

To begin, you add persistence code for border values to the drawBorder_Click() event handler, where those values are first assigned. Then, in the ConfigureCrystalReports() method, the values that are stored in Session are retrieved and assigned to the respective property of the CrystalReportViewer class.

To add Session assignment code to the drawBorder_Click() event handler

  1. In the drawBorder_Click() event handler, following the existing code add four Session assignments for BackColor, BorderColor, BorderStyle, and BorderWidth.
    [Visual Basic]
    Session("myBorderColor") = myCrystalReportViewer.BorderColor.ToString()
    Session("myBorderStyle") = myCrystalReportViewer.BorderStyle
    Session("myBorderWidth") = myCrystalReportViewer.BorderWidth
    [C#]
    Session["borderColor"] = crystalReportViewer.BorderColor.ToString();
    Session["borderStyle"] = crystalReportViewer.BorderStyle;
    Session["borderWidth"] = crystalReportViewer.BorderWidth;

You are now ready to retrieve these values out of Session in the ConfigureCrystalReports() method.

To add Session retrieval code to the ConfigureCrystalReports() method

  1. At the bottom of the ConfigureCrystalReports() method, create an If block that checks whether the BackColor Session variable is not null. If it is not, then within the If block, retrieve the BackColor property from Session and cast it to a string. Pass the string to the FromName() method of the Color class and assign the Color instance to the BackColor property of the CrystalReportViewer instance.
    [Visual Basic]
    myCrystalReportViewer.BackColor = Color.FromName(CType(Session("myBackColor"), String))
    [C#]
    if (Session["backColor"] != null)
    {
        crystalReportViewer.BackColor = Color.FromName((string)Session["backColor"]);
    }
  2. Create a second If block that checks whether the BorderColor Session variable is not null. If it is not, then within the If block, retrieve the BorderColor property from Session and cast it to a string. Pass the string to the FromName() method of the Color class and assign the Color instance to the BorderColor property of the CrystalReportViewer instance.
    [Visual Basic]
    myCrystalReportViewer.BorderColor = Color.FromName(CType(Session("myBorderColor"), String))
    [C#]
    if (Session["borderColor"] != null)
    {
        crystalReportViewer.BorderColor = Color.FromName((string)Session["borderColor"]);
    }
  3. Create a third If block that checks whether the BorderStyle Session variable is not null. If it is not, then within the If block, retrieve the BorderStyle property from Session and cast it to BorderStyle.
    [Visual Basic]
    myCrystalReportViewer.BorderStyle = CType(Session("myBorderStyle"), BorderStyle)
    [C#]
    if (Session["borderStyle"] != null)
    {
        crystalReportViewer.BorderStyle = (BorderStyle)Session["borderStyle"];
    }
  4. Create a fourth If block that checks whether the BorderWidth Session variable is not null. If it is not, then within the If block, retrieve the BorderWidth property from Session and convert it to an integer.
    [Visual Basic]
    myCrystalReportViewer.BorderWidth = Convert.ToInt32(Session("myBorderWidth"))
    [C#]
    if (Session["borderStyle"] != null)
    {
        crystalReportViewer.BorderWidth = Convert.ToInt32(Session["borderStyle"]);
    }

In the next procedure, you add Session persistence to the redisplay Button click event handler.

To code the redisplay Button control

  • Within the redisplay_Click() event handler, assign the selected item from the selectBackColor DropDownList to Session.
    [Visual Basic]
    Session("myBackColor") = selectBackColor.SelectedItem.Text
    [C#]
    Session["backColor"] = selectBackColor.SelectedItem.Text;

To code the drawBorder Button control

  1. Within the drawBorder_Click() event handler, assign the Text property of the borderWidth TextBox to Session.
    [Visual Basic]
    Session("myBorderWidth") = borderWidth.Text
    [C#]
    Session["borderWidth"] = borderWidth.Text;
  2. Assign the selected index from the selectBorderStyle DropDownList to Session.
    [Visual Basic]
    Session("myBorderStyle") = selectBorderStyle.SelectedIndex
    [C#]
    Session["borderStyle"] = selectBorderStyle.SelectedIndex;
  3. Assign the selected item from the selectBorderColor DropDownList to Session.
    [Visual Basic]
    Session("myBorderColor") = selectBorderColor.SelectedItem.Text
    [C#]
    Session["borderColor"] = selectBorderColor.SelectedItem.Text;

You are now ready to build and run the project, to verify that the changes made to the report are persisted through button click events.

To test the project, you can follow the instructions listed in the procedures at the top of this section.

To learn about modifying the visual appearance of the CrystalReportViewer toolbar, continue to Modifying Graphics and Cascading Style Sheets.

See Also

Tutorials: Customizing the CrystalReportViewer Control | Tutorials and Sample Code | Tutorials' Sample Code Directory

Show:
© 2015 Microsoft