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.ascx的 Web 部件用户控件的.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 控件。

Important

此示例有一个接受用户输入的文本框,这是潜在的安全威胁。 默认情况下,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, Boolean)

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

PersonalizableAttribute(PersonalizationScope)

使用提供的参数初始化类的新实例 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)

适用于