When the Persistence manager is looping through the controls in a page, it saves all of them by default. However, if you want to persist just one group of controls, you have the option of excluding specific controls. Excluding controls reduces the size of the persisted data, which can sometimes be crucial for the application of available resources.
The Persistence framework API contains classes that hold the persisted data. The class that contains the data for the whole page is PersistenceData, and for a single control is * ControlData* .
You can remove the unnecessary controls while handling the * PersistenceSaved* event. To do so, you must list all the controls that you want to be ignored in an array and provide to the Persistence manager.
In ASPX: |
protected void PersistenceSaved(object sender, PersistenceEventArgs e)
{
if (e.PersistenceData == null)
{
return;
}
// List of all the control IDs that will not be persisted
List<string> ignoredControls = new List<string>() { this.control1.ID,
this.control2.ID,
this.control3.ID };
PersistenceData data = e.PersistenceData;
ControlData controlToRemove = null;
foreach (string ignoredControlID in ignoredControls)
{
controlToRemove = data.Controls.Find(
delegate(ControlData controlToFind)
{
return
controlToFind.ControlId == ignoredControlID;
});
if (controlToRemove != null)
{
data.Controls.Remove(controlToRemove);
controlToRemove = null;
}
}
this.StoredData = data.ToJson();
}
|
This sample is appropriate to use in situations where you exclude a small number of controls. If you remove most of the controls, you can easily use the Persistence manager to save the controls manually. You can follow Persist control manually topic to see more information about manual persistence.