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 之網頁元件使用者控制項的 .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 Web 網頁會驗證使用者輸入未包含指令碼或 HTML 項目。 如需詳細資訊,請參閱 Script Exploits Overview (指令碼攻擊概觀)。

<%@ 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 會套用至需要保存個人化資訊的公用控制項屬性。 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)

將一組名稱對應至一組對應的分派識別項 (Dispatch Identifier)。

(繼承來源 Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

擷取物件的類型資訊,可以用來取得介面的類型資訊。

(繼承來源 Attribute)
_Attribute.GetTypeInfoCount(UInt32)

擷取物件提供的類型資訊介面數目 (0 或 1)。

(繼承來源 Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供物件所公開的屬性和方法的存取權。

(繼承來源 Attribute)

適用於