Version

Handling Persistence Events

The Ultimate UI for ASP.NET Persistence Framework component has four types of events which occur at a different time during the page lifecycle and feature specific arguments. Two of the events are fired during the process of saving persisted data while the other events are fired during data loading.

PersistenceLoading Event

The * PersistenceLoading* event is fired after the Page’s PageLoad event. During PageLoad, persisted data is reliably available to the Persistence Framework. In the event of post back, the controls’ state is restored from ViewState, but if the page is requested for first time, then Persistence Framework steps in to restore state - simulating the behavior of the ViewState.

The PersistenceLoading event is fired on the first request to the page depending on the value of the attribute persistenceIgnoreLoadingOnPostBack in the Web.config file. If the value is false, the event is fired on every request to the page including postbacks. In some cases when you use ViewState it saves the state of the controls during the postbacks. That’s why setting the persistenceIgnoreLoadingOnPostBack to true increases performance because the Persistence Loading event fires only on the first request.

Listing 1: Create LoadingHandlers in code behind

In C#:
protected void LoadingHandler(object sender, PersistenceLoadingEventArgs e) {}
In Visual Basic:
Protected Sub LoadingHandler(sender As Object, e As PersistenceLoadingEventArgs)End Sub

The following items describe the values expected in LoadingHandler method arguments as shown in Listing 1 :

  • sender - references the current page

  • e – The type of the event is * PersistenceLoadingEventArgs. It inherits .NET *CancelEventArgs object. One general purpose is to use this event to cancel the process of loading the data for your particular needs. PersistenceLoadingEventArgs also wraps the persisted data that needs to be loaded.

Listing 2 displays the PersistenceLoadingEventArgs properties used to access data to persist as well as exposing an option to cancel the event.

Listing 2: PersistenceLoadingEventArgs*properties

In C# and Visual Basic:
e.Cancel e.PersistenceData

Persistence Loaded Event

The * PersistenceLoaded* event is fired after the PersistenceLoading event. At this stage of the cycle, the Persistence Framework has restored the persisted data with all the controls’ state. If the restore fails due to an exception, inside the PersistenceLoaded handler you can process that error.

Listing 3: Create LoadedHandler in code behind

In C#:
protected void LoadedHandler (object sender, PersistenceEventArgs e) {}
In Visual Basic:
Protected Sub LoadedHandler (sender As Object, e As PersistenceEventArgs)End Sub

The following items describe the values expected in LoadedHandler method arguments as shown in Listing 3 :

  • sender - references the current page

  • e – The type of the event is * PersistenceEventArgs* . The persistence data it carries is an exception, which may occur during the loading of the persisted data.

Listing 4 displays the PersistenceEventArgs properties used to access data to persist as well as exposing access to any exceptions that may have occurred while raising the event.

Listing 4: PersistenceEventArgs properties

In C# and Visual Basic:
e.Exception e.PersistenceData
Note
Note:

If during the Persistence Loading event there is no persisted data to retrieve, the Persistence Loaded event does not fire at all.

Persistence Saving Event

The * PersistenceSaving* event is fired after the SaveStateComplete event completes. Both events, including the PersistenceSaved event, are fired every time the page is requested. At this stage, the Persistence Framework is preserving the state of all the controls. As the event’s EventArgs parameter inherits .NET CancelEventArgs object, you can cancel the process of saving for your particular needs.

Listing 5: Create the SavingHandler in code behind

In C#:
protected void SavingHandler(object sender, PersistenceSavingEventArgs e) {}
In Visual Basic:
Protected Sub SavingHandler(sender As Object, e As PersistenceSavingEventArgs)End Sub

The following items describe the values expected in SavingHandler method arguments as shown in Listing 5 :

  • sender - references the current page

  • e – The type of the event is * PersistenceSavingEventArgs* . It inherits .NET CancelEventArgs object. This means you can cancel the process of saving data if you have cases where you don’t want to persist with the page.

Listing 6 displays the PersistenceSavingEventArgs property to expose an option to cancel the event.

Listing 6: PersistenceSavingEventArgs properties

In C# and Visual Basic:
e.Cancel

Persistence Saved Event

The * PersistenceSaved* event fires after the PersistenceSaving one. Again, as in the PersistenceLoaded event, if an exception occurs during the persisting of the data, it can be handled. Listing 7 shows the structure for where you can save persisted data to a stable source (file, Session, Application, or SQL). Before ultimately saving the data, you can modify it or remove or add controls and properties.

Listing 7: Create SavedHandler in code behind

In C#:
protected void SavedHandler(object sender, PersistenceEventArgs e) {}
In Visual Basic:
Protected Sub SavedHandler(sender As Object, e As PersistenceEventArgs)End Sub

The following items describe the values expected in SavedHandler method arguments as shown in Listing 5 :

  • sender - references the current page

  • e – The type of the event is * PersistenceEventArgs. Except the persistence data it carries may be an exception, which may occur during the saving of the persisted data. During the *PersistenceSaved event you can retrieve the persisted data into a collection or to exclude or include some data in it.

Listing 8 displays the PersistenceEventArgs properties used to access data to persist as well as exposing access to any exceptions that may have occurred while raising the event.

Listing 8: PersistenceEventArgs properties

In C# and Visual Basic:
e.Exception e.PersistenceData