Share via


ViewState and Persistence of the CrystalReportViewer Object Model

What is ViewState?

ViewState is a browser-based approach in ASP.NET for persisting the state of the view, that is, the Web Form. Its primary function is to support the persistence of Web controls.

Web controls (also called Web server controls) are modeled on Windows controls, which were introduced in Visual Basic. Windows controls are objects on the form that encapsulate a piece of display functionality, such as a text field, a button, or a table of data.

Web controls are similar to Windows controls. Like Windows controls, they operate on two levels: within the Web page, and in the code-behind class that supports the Web page. Similar to traditional controls in Windows Forms, Web controls encapsulate discrete pieces of display functionality into GUI objects: Button, TextField, DropDownList, DataGrid, and so on. In the code-behind class, these same controls are paralleled as classes that expose properties and methods.

A Web page is different from a Windows form in that a Web page is a stateless environment. Therefore some kind of persistence mechanism is required to preserve the state of the Web page across page reloads.

ViewState maintains the state of controls on the Web page, much as Session maintains the state of instantiated objects on the server.

Note

ViewState maintains the state of all Web controls automatically. This is achieved by having ViewState store each control based on the control's EnableViewState property (which defaults to True).

Because ViewState stores the state of the data from the Web controls on the page, the entire ViewState object must be enclosed within the page as the page is transferred back and forth between the browser and the Web server. This is done by encrypting the entire ViewState object as a string, and then placing this string within the value of a hidden form tag on the page. For example, the ViewState of an ASP.NET Web page that contains only a single button control has the following HTML code:

<input type="hidden" name="__VIEWSTATE"
value="dDwtNTMwNzcxMzI0Ozs+I7GfLyg3p44eTLFCiVEiRKUBzFw=" />

ViewState stores only information that can be converted to string format.

Persisting the report display of the CrystalReportViewer control

The CrystalReportViewer control performs the role of report display for a Crystal report. It renders the report into html on the page along with a toolbar and tree view for manipulating the report display. The toolbar contains buttons to zoom, go to next page, print, export, and so on. The tree view expands to show nested grouping of data.

ViewState persists control information; therefore it persists the state of all report display information (including toolbar and tree view events) for the CrystalReportViewer control across page reloads.

For example, if a user were viewing page 3 of the report and clicked the next page button in the toolbar of the CrystalReportViewer control, ViewState would persist the state of both pieces of information:

  • The current page number.
  • The state of the Next Page button (clicked).

During page reload, the ViewState would restore the CrystalReportViewer control to page 3, and then restore the next page event click, causing the control to move the report ahead to page 4.

Persisting the object model of the CrystalReportViewer control

The CrystalReportViewer control performs an additional role: not only report display, but also a limited object model (contained in the CrystalReportViewer control class). This limited object model can be used for programmatic interaction with the report.

ViewState persists the state of both roles:

  • The report display.
  • The CrystalReportViewer object model.

However, use of the CrystalReportViewer object model is generally discouraged, in favor of the more extensive ReportDocument object model. This alternate object model is not contained within the control but is part of the class libraries in the SDK.

Note

For more information, see Choose the Correct Object Model for Your Project.

Sharing persistence mechanisms

If you use the CrystalReportViewer control to perform both roles (report display and object model), ViewState persists both of them, and you do not need any additional persistence mechanisms.

Note

An example of this would be binding the CrystalReportViewer control to a file directory path. See Binding to a File Directory Path in Code.

However, if you choose to use the CrystalReportViewer control only for the role of report display, and then bind the control to an external object model (such as ReportDocument), a separate persistence mechanism is required to persist that external object model. Typically that second persistence mechanism is Session (or occasionally, Cache).

See Also