You can implement the IPersonalizable interface and manage your custom properties in those functions. Here's a basic example:
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
namespace CustomControls
{
public class SimpleIPersonalizableWebPart : WebPart, IPersonalizable
{
private TextBox txtBox;
public SimpleIPersonalizableWebPart()
{
txtBox = new TextBox();
this.Title = "Default SimpleIPersonalizableWebPart Title";
}
protected override void CreateChildControls()
{
txtBox.ID = "txtId";
txtBox.Text = "Default textbox Text";
Controls.Add(txtBox);
}
void IPersonalizable.Load(PersonalizationDictionary state)
{
if (Controls.Count == 0 || state == null)
return;
PersonalizationEntry entry = state["TextBoxTextValue"];
if (entry != null)
{
object obj = entry.Value;
if (obj.GetType().Name == "String" && Controls[0].GetType().Name == "TextBox")
{
((TextBox)Controls[0]).Text = (string)obj;
}
}
}
void IPersonalizable.Save(PersonalizationDictionary state)
{
foreach (Control control in Controls)
{
if (control.GetType().Name == "TextBox")
{
PersonalizationEntry entry = new PersonalizationEntry(((TextBox)control).Text, PersonalizationScope.User);
state.Add("TextBoxTextValue", entry);
}
}
}
bool IPersonalizable.IsDirty
{
get
{
return true;
}
}
}
}