PersonalizableAttribute 类

定义

表示个性化特性。 此类不能被继承。

public ref class PersonalizableAttribute sealed : Attribute
[System.AttributeUsage(System.AttributeTargets.Property)]
public sealed class PersonalizableAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Property)>]
type PersonalizableAttribute = class
    inherit Attribute
Public NotInheritable Class PersonalizableAttribute
Inherits Attribute
继承
PersonalizableAttribute
属性

示例

下面的代码示例演示如何在代码中使用 PersonalizableAttribute 类。 该示例包含引用名为 的 ColorSelector.ascxWeb 部件用户控件的 .aspx 页。 以下代码是此示例的 .aspx 文件。

<%@ 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>
         <asp:LoginName ID="LoginName1" runat="server" />
         
        <asp:LoginStatus ID="LoginStatus1" runat="server" LogoutAction="RedirectToLoginPage" />
            <br />
        <br />
         <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>

以下代码适用于 ColorSelector.ascx 控件。

重要

此示例具有一个接受用户输入的文本框,这是一个潜在的安全威胁。 默认情况下,ASP.NET 网页验证用户输入是否不包含脚本或 HTML 元素。 有关详细信息,请参阅脚本侵入概述

<%@ 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>
    <div>
        <asp:TextBox ID="_color" runat="server" Height="100" Width="100" />
        <br />
        <asp:button runat="server"  id="_redButton" text="Red"  /> 
          
        <asp:button runat="server"  id="_greenButton" text="Green" />
          
        <asp:button runat="server" id="_blueButton" text="Blue" />
    </div>
</body>

注解

个性化设置属性 Personalizable应用于需要保留个性化设置信息的公共控件属性。 当控件位于 Web 部件页上的 Web 部件区域中时,ASP.NET 自动生成代码,以便从基础数据存储中保存或检索这些值。

若要将属性标记为可个性化,必须满足以下要求:

  • 属性必须是公共的,并且必须具有公共 get 和 set 访问器。

  • 属性必须是读/写属性。

  • 属性必须不带参数。

  • 无法为属性编制索引。

自动生成代码以加载和保存属性的个性化数据。 支持个性化设置的属性是根据属性上是否存在此属性以及属性符合上面列出的约束的事实来确定的。

请注意,只读和只写属性不支持个性化设置。 将此属性应用于只读或只写属性会导致 HttpException 引发 。 参数化属性也会引发 HttpException 异常。

如果没有通过 IPersonalizable 接口使用特殊处理,则不具有此属性的单个属性将从个性化中排除。

有关使用属性的详细信息,请参阅 Web 部件个性化概述

构造函数

PersonalizableAttribute()

初始化 PersonalizableAttribute 类的新实例。

PersonalizableAttribute(Boolean)

使用提供的参数初始化 PersonalizableAttribute 类的新实例。

PersonalizableAttribute(PersonalizationScope)

使用提供的参数初始化 PersonalizableAttribute 类的新实例。

PersonalizableAttribute(PersonalizationScope, Boolean)

使用提供的参数初始化 PersonalizableAttribute 类的新实例。

字段

Default

返回一个指示不支持个性化的特性实例。 此字段为只读。

NotPersonalizable

返回一个指示不支持个性化的特性实例。 此字段为只读。

Personalizable

返回一个指示支持个性化的特性实例。 此字段为只读。

SharedPersonalizable

返回一个特性实例,它指示支持具有共享范围的个性化。 此字段为只读。

UserPersonalizable

返回一个特性实例,它指示在 User 范围支持个性化。 此字段为只读。

属性

IsPersonalizable

获取指示该特性是否可进行个性化设置的设置(此设置是由一个构造函数建立的)。

IsSensitive

获取指示该特性是否属于敏感信息的设置(此设置是由一个构造函数建立的)。

Scope

获取类实例的 PersonalizationScope 枚举值(枚举值是由一个构造函数设置的)。

TypeId

在派生类中实现时,获取此 Attribute 的唯一标识符。

(继承自 Attribute)

方法

Equals(Object)

当重写时,返回 PersonalizableAttribute 的当前实例和作为参数提供的另一个 PersonalizableAttribute 实例的布尔型计算数值。

GetHashCode()

当重写时,返回该特性的哈希代码。

GetPersonalizableProperties(Type)

返回特定属性的 PropertyInfo 对象的集合,这些属性与此参数类型匹配并标记为可个性化。

GetType()

获取当前实例的 Type

(继承自 Object)
IsDefaultAttribute()

当重写时,返回一个值,该值指示此特性实例是否等于静态 Default 字段的值。

Match(Object)

返回一个值,该值指示 PersonalizableAttribute 的当前实例和指定的 PersonalizableAttribute 是否具有相同的 IsPersonalizable 属性值。

MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
ToString()

返回表示当前对象的字符串。

(继承自 Object)

显式接口实现

_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

将一组名称映射为对应的一组调度标识符。

(继承自 Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

检索对象的类型信息,然后可以使用该信息获取接口的类型信息。

(继承自 Attribute)
_Attribute.GetTypeInfoCount(UInt32)

检索对象提供的类型信息接口的数量(0 或 1)。

(继承自 Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供对某一对象公开的属性和方法的访问。

(继承自 Attribute)

适用于