共用方式為


ProfileBase 類別

定義

提供無需類型存取的個人資料、屬性、價值與資訊。

public ref class ProfileBase : System::Configuration::SettingsBase
public class ProfileBase : System.Configuration.SettingsBase
type ProfileBase = class
    inherit SettingsBase
Public Class ProfileBase
Inherits SettingsBase
繼承
ProfileBase
衍生

範例

以下程式碼範例展示了一個 Web.config 檔案,指定一個包含 ZipCode 屬性 stringRecentSearchListStringCollection為 的屬性 和 屬性的使用者設定檔。 電流產生 Profile 的屬性 HttpContext 會對每個指定屬性有強型別的存取器。

<configuration>
  <system.web>
    <anonymousIdentification enabled="true" />

    <profile enabled="true" defaultProvider="SqlProvider" >
      <properties>
        <add name="ZipCode" allowAnonymous="true" />
        <add name="RecentSearchList"
          type="System.Collections.Specialized.StringCollection"
          serializeAs="Xml"
          allowAnonymous="true" />
      </properties>
    </profile>
  </system.web>
</configuration>

以下範例顯示一個 ASP.NET 頁面,讀取並設定 ZipCode 使用者設定檔指定的屬性。 在嘗試執行此程式碼前,請在網站的 ASP.NET 設定中將提供者設為預設 AspNetSqlProvider 值。

<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

public void Page_PreRender()
{
  if (Profile.ZipCode == null)
  {
    PersonalizePanel.Visible = false;
    GetZipCodePanel.Visible = true;
  }
  else
  {
    ZipCodeLabel.Text = Profile.ZipCode;

    // Get personalized information for zip code here.

    PersonalizePanel.Visible = true;
    GetZipCodePanel.Visible = false;
  }
}

public void ChangeZipCode_OnClick(object sender, EventArgs args)
{
  ZipCodeTextBox.Text = Profile.ZipCode;
  Profile.ZipCode = null;

  PersonalizePanel.Visible = false;
  GetZipCodePanel.Visible = true;
}

public void EnterZipCode_OnClick(object sender, EventArgs args)
{
  Profile.ZipCode = ZipCodeTextBox.Text;
}

</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Home Page</title>
</head>
<body>

<form id="form1" runat="server">
  <table border="1" cellpadding="2" cellspacing="2">
    <tr>
      <td>
        <asp:Panel id="PersonalizePanel" runat="Server" Visible="False">
          Information for Zip Code: <asp:Label id="ZipCodeLabel" Runat="Server" /><br />
          <!-- Information for Zip Code here. -->
          <br />
          <asp:LinkButton id="ChangeZipCodeButton" Runat="Server" Text="Change Your Zip Code"
                          OnClick="ChangeZipCode_OnClick" />
        </asp:Panel>
        <asp:Panel id="GetZipCodePanel" runat="Server" Visible="False">
          You can personalize this page by entering your Zip Code: 
          <asp:TextBox id="ZipCodeTextBox" Columns="5" MaxLength="5" runat="Server" />
          <asp:LinkButton id="EnterZipCodeButton" Runat="Server" Text="Go"
                          OnClick="EnterZipCode_OnClick" />
        </asp:Panel>
      </td>
    </tr>
  </table>
</form>

</body>
</html>
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

Public Sub Page_PreRender()

  If Profile.ZipCode = Nothing Then
    PersonalizePanel.Visible = False
    GetZipCodePanel.Visible = True
  Else
    ZipCodeLabel.Text = Profile.ZipCode

    ' Get personalized information for zip code here.

    PersonalizePanel.Visible = True
    GetZipCodePanel.Visible = False
  End If

End Sub

Public Sub ChangeZipCode_OnClick(sender As Object, args As EventArgs)
  ZipCodeTextBox.Text = Profile.ZipCode
  Profile.ZipCode = Nothing

  PersonalizePanel.Visible = False
  GetZipCodePanel.Visible = True
End Sub

Public Sub EnterZipCode_OnClick(sender As Object, args As EventArgs)
  Profile.ZipCode = ZipCodeTextBox.Text
End Sub

</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Home Page</title>
</head>
<body>

<form id="form1" runat="server">
  <table border="1" cellpadding="2" cellspacing="2">
    <tr>
      <td>
        <asp:Panel id="PersonalizePanel" runat="Server" Visible="False">
          Information for Zip Code: <asp:Label id="ZipCodeLabel" Runat="Server" /><br />
          <!-- Information for Zip Code here. -->
          <br />
          <asp:LinkButton id="ChangeZipCodeButton" Runat="Server" Text="Change Your Zip Code"
                          OnClick="ChangeZipCode_OnClick" />
        </asp:Panel>
        <asp:Panel id="GetZipCodePanel" runat="Server" Visible="False">
          You can personalize this page by entering your Zip Code: 
          <asp:TextBox id="ZipCodeTextBox" Columns="5" MaxLength="5" runat="Server" />
          <asp:LinkButton id="EnterZipCodeButton" Runat="Server" Text="Go"
                          OnClick="EnterZipCode_OnClick" />
        </asp:Panel>
      </td>
    </tr>
  </table>
</form>

</body>
</html>

以下程式碼範例定義了一個繼承自該 ProfileBase 類別以建立自訂設定檔的類別。 自訂設定檔的類型會在應用程式 Web.config 檔案中設定檔設定元素的屬性中指定inherits

這很重要

此範例包含一個接受使用者輸入的文字框,這可能構成安全威脅。 預設情況下,ASP.NET 網頁會驗證使用者輸入中不包含腳本或 HTML 元素。 欲了解更多資訊,請參閱 腳本漏洞概述

using System;
using System.Web.Profile;

namespace Samples.AspNet.Profile
{
  public class EmployeeProfile : ProfileBase
  {
    [SettingsAllowAnonymous(false)]
    [ProfileProvider("EmployeeInfoProvider")]
    public string Department
    {
      get { return base["EmployeeDepartment"].ToString(); }
      set { base["EmployeeDepartment"] = value; }
    }

    [SettingsAllowAnonymous(false)]
    [ProfileProvider("EmployeeInfoProvider")]
    public EmployeeInfo Details
    {
      get { return (EmployeeInfo)base["EmployeeInfo"]; }
      set { base["EmployeeInfo"] = value; }
    }
  }

  public class EmployeeInfo
  {
    public string Name;
    public string Address;
    public string Phone;
    public string EmergencyContactName;
    public string EmergencyContactAddress;
    public string EmergencyContactPhone;
  }
}
Imports System.Web.Profile

Namespace Samples.AspNet.Profile

  Public Class EmployeeProfile
    Inherits ProfileBase

    <SettingsAllowAnonymous(False)> _
    <ProfileProvider("EmployeeInfoProvider")> _
    Public Property Department As String
      Get
        Return MyBase.Item("EmployeeDepartment").ToString()
      End Get
      Set
        MyBase.Item("EmployeeDepartment") = value
      End Set
    End Property

    <SettingsAllowAnonymous(False)> _
    <ProfileProvider("EmployeeInfoProvider")> _
    Public Property Details As EmployeeInfo
      Get
        Return CType(MyBase.Item("EmployeeInfo"), EmployeeInfo)
      End Get
      Set
        MyBase.Item("EmployeeInfo") = value
      End Set
    End Property
  End Class

  Public Class EmployeeInfo
    Public Name As String
    Public Address As String
    Public Phone As String
    Public EmergencyContactName As String
    Public EmergencyContactAddress As String
    Public EmergencyContactPhone As String
  End Class

End Namespace

備註

ASP.NET 會用這個 ProfileBase 類別來建立用於使用者設定檔的類別。 當啟用使用者設定檔的應用程式啟動時,ASP.NET 會建立一個新的類型 ProfileCommon類別,該類別繼承自該 ProfileBase 類別。 在設定檔設定區段中定義的每個屬性,都會為類別加入ProfileCommon強型別的存取器。 該類別的ProfileCommon強型別存取器呼叫GetPropertyValue基底類別的 ProfileBaseSetPropertyValue 方法,分別取得 profile 屬性值並設定。 類別的 ProfileCommon 一個實例被設定為該屬性的值 Profile ,用於 ASP.NET 應用。

建議在 ASP.NET 應用程式 Create 中建立使用者設定檔實例。

給繼承者的注意事項

你可以建立一個自訂的設定檔實作,繼承自ProfileBase抽象類別,並為使用者設定檔定義設定中未指定的屬性。 你可以在 web.config 檔案中指定自訂的使用者設定檔類型,並設定inherits設定檔設定元素的屬性,如下範例所示。 該類別的程式碼 EmployeeProfile 包含在本主題的範例部分。

<configuration>
   <system.web>
      <profile inherits="Samples.AspNet.Profile.EmployeeProfile"  
      defaultProvider="SqlProvider">  
      <providers>  
         <clear />  
         <add  
            name="SqlProvider"  
            type="System.Web.Profile.SqlProfileProvider"   
            connectionStringName="SqlServices"   
            description="SQL Profile Provider for Sample"/>   
         <add  
            name="EmployeeInfoProvider"  
            type="System.Web.Profile.SqlProfileProvider"   
            connectionStringName="SqlServices"   
            description="SQL Profile Provider for Employee Info"/>   
      </providers>  
      
      <properties>  
         <add name="GarmentSize" />  
      </properties>  
      </profile>  
   </system.web>
</configuration>

建構函式

名稱 Description
ProfileBase()

建立 ProfileBase 類別的執行個體。

屬性

名稱 Description
Context

能取得相關的設定上下文。

(繼承來源 SettingsBase)
IsAnonymous

會獲得一個值,表示該用戶檔案是否屬於匿名使用者。

IsDirty

會得到一個值,表示是否有任何設定檔屬性被修改過。

IsSynchronized

會取得一個值,表示對物件的存取是否同步(執行緒安全)。

(繼承來源 SettingsBase)
Item[String]

取得或設定以房產名稱為索引的房產價值。

LastActivityDate

會取得該個人資料被閱讀或修改的最新日期和時間。

LastUpdatedDate

會取得個人資料被修改的最新日期和時間。

Properties

會為設定檔中的每個屬性取得一組 SettingsProperty 物件。

PropertyValues

會取得一組設定屬性值。

(繼承來源 SettingsBase)
Providers

會取得一系列設定提供者。

(繼承來源 SettingsBase)
UserName

會取得個人檔案的使用者名稱。

方法

名稱 Description
Create(String, Boolean)

ASP.NET 用來建立指定使用者名稱的個人檔案實例。 會取一個參數來判斷使用者是被認證還是匿名。

Create(String)

ASP.NET 用來建立指定使用者名稱的個人檔案實例。

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設哈希函式。

(繼承來源 Object)
GetProfileGroup(String)

獲得一組以群組名稱識別的屬性。

GetPropertyValue(String)

取得某個特徵房產的價值。

GetType()

取得目前實例的 Type

(繼承來源 Object)
Initialize(SettingsContext, SettingsPropertyCollection, SettingsProviderCollection)

初始化物件 SettingsBase 所使用的內部屬性。

(繼承來源 SettingsBase)
Initialize(String, Boolean)

初始化當前使用者的設定檔屬性值與資訊。

MemberwiseClone()

建立目前 Object的淺層複本。

(繼承來源 Object)
Save()

更新設定檔資料來源,並更改設定檔屬性值。

SetPropertyValue(String, Object)

設定設定檔屬性的值。

ToString()

傳回表示目前 物件的字串。

(繼承來源 Object)

適用於

另請參閱