Export (0) Print
Expand All

PersonalizableAttribute Class

Note: This class is new in the .NET Framework version 2.0.

Represents the personalization attribute. This class cannot be inherited.

Namespace: System.Web.UI.WebControls.WebParts
Assembly: System.Web (in system.web.dll)

[AttributeUsageAttribute(AttributeTargets.Property)] 
public sealed class PersonalizableAttribute : Attribute
/** @attribute AttributeUsageAttribute(AttributeTargets.Property) */ 
public final class PersonalizableAttribute extends Attribute
AttributeUsageAttribute(AttributeTargets.Property) 
public final class PersonalizableAttribute extends Attribute

The personalization attribute, Personalizable, is applied to public control properties that need to persist personalization information. ASP.NET automatically generates the code to persist or retrieve these values from the underlying data store when the control is in a Web Parts zone on a Web Parts page.

The following requirements must be met for a property to be marked as personalizable:

  • The property must be public and must have public get and set accessors.

  • The property must be a read/write property.

  • The property must be without parameters.

  • The property cannot be indexed.

Code is automatically generated to load and save personalization data for properties. Properties that support personalization are determined based on the existence of this attribute on the property and the fact that the property conforms to the constraints listed above.

Note that read-only and write-only properties are not supported for personalization. Applying this attribute to a read-only or write-only property results in an HttpException being thrown. Parameterized properties also throw an HttpException exception.

Individual properties without this attribute are excluded from personalization if no special handling through the IPersonalizable interface is used.

For more information about using attributes, see Web Parts Personalization Overview.

The following code example demonstrates how to use the PersonalizableAttribute class in code. The sample consists of an .aspx page that references a Web Part user control called ColorSelector.ascx. The following code is the .aspx file for the example.

<%@ Page Language="C#"  %>
<%@ Register TagPrefix="uc1" TagName="colorcontrol" Src="ColorSelector.ascx" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
       
<body>
    <form id="form1" runat="server">
      <div>
        &nbsp;<asp:LoginName ID="LoginName1" runat="server" />
        &nbsp;
        <asp:LoginStatus ID="LoginStatus1" runat="server" LogoutAction="RedirectToLoginPage" />
        &nbsp;&nbsp;&nbsp;&nbsp;<br />
        <br />
        &nbsp;<asp:WebPartManager ID="WebPartManager1" runat="server">
        </asp:WebPartManager>
    
    </div>
        <asp:WebPartZone ID="WebPartZone1" runat="server"  Height=200 Width=200>
        <ZoneTemplate>
        <uc1:colorcontrol id=colorcontrol runat=server />
        </ZoneTemplate>
        </asp:WebPartZone>        
    </form>
</body>
</html>

The following code is for the ColorSelector.ascx control.

<%@ Control Language="C#" %>

<script runat=server>
private System.Drawing.Color userchoice;

[Personalizable]
public System.Drawing.Color UserColorChoice
{
   get
   {
     return userchoice;
   }
   set
   {
     userchoice = value;
   }
}

protected void OnRed(object src, EventArgs e)
{
  _color.BackColor = System.Drawing.Color.Red;
  UserColorChoice = System.Drawing.Color.Red;
}

protected void OnGreen(object src, EventArgs e)
{
  _color.BackColor = System.Drawing.Color.Green;
  UserColorChoice = System.Drawing.Color.Green;
}

protected void OnBlue(object src, EventArgs e)
{
  _color.BackColor = System.Drawing.Color.Blue;
  UserColorChoice = System.Drawing.Color.Blue;
}

protected void Page_Init(object src, EventArgs e)
{
  _redButton.Click   += new EventHandler(OnRed);  
  _greenButton.Click += new EventHandler(OnGreen);  
  _blueButton.Click  += new EventHandler(OnBlue);  
}

protected void Page_Load(object src, EventArgs e)
{
  if (!IsPostBack)
  {
          _color.BackColor = UserColorChoice;
  }
}

</script>
<body>
    
    <asp:TextBox ID=_color runat=server Height=100 Width=100 />
    <p>
    <asp:button runat=server  id=_redButton text="Red"  /> 
    &nbsp;&nbsp;
    <asp:button runat=server  id=_greenButton text="Green" />
    &nbsp;&nbsp;
    <asp:button runat=server id=_blueButton text="Blue" />
    
</body>

System.Object
   System.Attribute
    System.Web.UI.WebControls.WebParts.PersonalizableAttribute

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0

Community Additions

ADD
Show:
© 2014 Microsoft