IPersonalizable.Save Method (PersonalizationDictionary)
Saves custom properties and internal state information in the control's PersonalizationDictionary object.
Assembly: System.Web (in System.Web.dll)
Parameters
- state
-
Type:
System.Web.UI.WebControls.WebParts.PersonalizationDictionary
A PersonalizationDictionary that contains custom scoped data that was loaded from the underlying data store.
A server control can store its own custom properties and internal state information the PersonalizationDictionary specified in the state parameter. The information is stored as a series of name/value pairs. It is the responsibility of the control to use name/value pairs that it can recognize during subsequent calls made to the control through the Load method.
A control can reference its associated WebPartManager control and check the PersonalizationScope object to determine the current scope. Custom state information should be appropriate for the current scope. Note that each PersonalizationEntry value that is added to the PersonalizationDictionary object should be associated with the appropriate scope value, because the personalization infrastructure depends on this when merging Shared and User-scoped custom data prior to passing it to the Load method.
When using the standard ASP.NET implementation of Web Parts, controls must ensure that objects placed in the state dictionary can be serialized by the ASP.NET ObjectStateFormatter class. In practice, this means the following:
Primitive .NET Framework types, strings, and collection-oriented .NET Framework types such as arrays, array lists, hash tables, and hybrid dictionaries, are automatically serializable.
Custom types that supply their own TypeConverter classes capable of serializing to and deserializing from strings are considered serializable.
Custom types that can be serialized by the BinaryFormatter class are considered serializable.
Important |
|---|
You should not add types based on classes defined in the App_Code directory and then depend on the default binary serialization mechanism. App_Code-based artifacts are not consistently binary-serializable due to the fact that they can have their assembly names changed at random points in time. |
The following code example demonstrates implementing the Save method in a custom WebPart control. For the full code required to run this example, see the Example section in the IPersonalizable class overview.
Imports System Imports System.Collections Imports System.ComponentModel Imports System.Diagnostics Imports System.Drawing Imports System.Web Imports System.Web.UI Imports System.Security.Permissions Imports System.Web.UI.WebControls Imports System.Web.UI.WebControls.WebParts Namespace Samples.AspNet.VB.Controls <AspNetHostingPermission(SecurityAction.Demand, _ Level:=AspNetHostingPermissionLevel.Minimal)> _ <AspNetHostingPermission(SecurityAction.InheritanceDemand, _ Level:=AspNetHostingPermissionLevel.Minimal)> _ Public Class UrlListWebPart Inherits WebPart Implements IPersonalizable Private _sharedUrls As ArrayList Private _userUrls As ArrayList Private _listDirty As Boolean Private _nameTextBox As TextBox Private _urlTextBox As TextBox Private _addButton As Button Private _list As BulletedList Protected Overrides Sub CreateChildControls() Dim nameLabel As New Label() Dim urlLabel As New Label() Dim breakLiteral1 As New LiteralControl("<br />") Dim breakLiteral2 As New LiteralControl("<br />") Dim breakLiteral3 As New LiteralControl("<br />") _nameTextBox = New TextBox() _urlTextBox = New TextBox() _addButton = New Button() _list = New BulletedList() nameLabel.Text = "Name: " urlLabel.Text = "URL: " _nameTextBox.ID = "nameTextBox" _urlTextBox.ID = "urlTextBox" _addButton.Text = "Add" _addButton.ID = "addButton" AddHandler _addButton.Click, AddressOf Me.OnClickAddButton _list.DisplayMode = BulletedListDisplayMode.HyperLink _list.ID = "list" Controls.Add(nameLabel) Controls.Add(_nameTextBox) Controls.Add(breakLiteral1) Controls.Add(urlLabel) Controls.Add(_urlTextBox) Controls.Add(breakLiteral2) Controls.Add(_addButton) Controls.Add(breakLiteral3) Controls.Add(_list) End Sub 'CreateChildControls Private Sub OnClickAddButton(ByVal sender As Object, ByVal e As EventArgs) Dim name As String = _nameTextBox.Text.Trim() Dim url As String = _urlTextBox.Text.Trim() Dim p As New Pair(name, url) If WebPartManager.Personalization.Scope = PersonalizationScope.Shared Then If _sharedUrls Is Nothing Then _sharedUrls = New ArrayList() End If _sharedUrls.Add(p) Else If _userUrls Is Nothing Then _userUrls = New ArrayList() End If _userUrls.Add(p) End If OnUrlAdded() End Sub 'OnClickAddButton Protected Overridable Sub OnUrlAdded() _listDirty = True ChildControlsCreated = False End Sub 'OnUrlAdded Protected Overrides Sub RenderContents(ByVal writer As HtmlTextWriter) If Not (_sharedUrls Is Nothing) Then Dim p As Pair For Each p In _sharedUrls _list.Items.Add(New ListItem(CStr(p.First), CStr(p.Second))) Next p End If If Not (_userUrls Is Nothing) Then Dim p As Pair For Each p In _userUrls _list.Items.Add(New ListItem(CStr(p.First), CStr(p.Second))) Next p End If MyBase.RenderContents(writer) End Sub 'RenderContents Public Overridable ReadOnly Property IsDirty() As Boolean _ Implements IPersonalizable.IsDirty Get Return _listDirty End Get End Property Public Overridable Shadows Sub Load(ByVal state As PersonalizationDictionary) _ Implements IPersonalizable.Load If Not (state Is Nothing) Then Dim sharedUrlsEntry As PersonalizationEntry = state("sharedUrls") If Not (sharedUrlsEntry Is Nothing) Then _sharedUrls = CType(sharedUrlsEntry.Value, ArrayList) End If Dim userUrlsEntry As PersonalizationEntry = state("userUrls") If Not (userUrlsEntry Is Nothing) Then _userUrls = CType(userUrlsEntry.Value, ArrayList) End If End If End Sub 'Load Public Overridable Sub Save(ByVal state As PersonalizationDictionary) _ Implements IPersonalizable.Save If Not (_sharedUrls Is Nothing) AndAlso _sharedUrls.Count <> 0 Then state("sharedUrls") = New PersonalizationEntry(_sharedUrls, PersonalizationScope.Shared) End If If Not (_userUrls Is Nothing) AndAlso _userUrls.Count <> 0 Then state("userUrls") = New PersonalizationEntry(_userUrls, PersonalizationScope.User) End If End Sub End Class End Namespace
Available since 2.0
