共用方式為


WebPartPersonalization 類別

定義

實作低階個人化作業。

public ref class WebPartPersonalization
[System.ComponentModel.TypeConverter(typeof(System.Web.UI.WebControls.EmptyStringExpandableObjectConverter))]
public class WebPartPersonalization
[<System.ComponentModel.TypeConverter(typeof(System.Web.UI.WebControls.EmptyStringExpandableObjectConverter))>]
type WebPartPersonalization = class
Public Class WebPartPersonalization
繼承
WebPartPersonalization
屬性

範例

下列程式代碼範例示範如何使用 WebPartPersonalization 類別,並提供個人化範圍如何影響個人化屬性結果的範例。 此範例有五個檔案:

  • 以宣告方式建立兩個網頁元件區域的.aspx頁面,每個區域都有控件。

  • 稱為 Color.ascx 的使用者控件,可讓使用者變更個人化屬性,根據使用者的授權許可權來修改狀態資訊並輸入共用範圍,套用文本框背景色彩。

  • 名為 Persmode.ascx 的使用者控件,指出目前頁面範圍是什麼,以及目前使用者必須輸入共用範圍或修改數據的許可權;它也會提供兩個按鈕;一個是變更範圍,另一個是重設目前使用者個人化資訊。

  • 用來授權使用者的登入控件。

  • 具有區段的 Web.config 檔案,提供授權使用者修改個人化狀態並輸入共用範圍的範例。

下列程式代碼會建立包含 和 WebPartManager 兩個網頁元件區域的 aspx 頁面,並顯示 Color.ascxPersmode.ascx 控件。 頁面會載入用來取得目前使用者資訊的登入頁面。 使用 Microsoft Visual Studio 2005 中的 ASP.NET 網站管理工具,建立使用者以存取頁面。 如需組態檔中授與授權的用戶範例,請參閱 Web.config 檔案,以變更範圍和修改狀態資訊。

<%@ Page Language="C#" %>
<%@ Register TagPrefix="control"  TagName="colorcontrol" Src="~/color.ascx"%>
<%@Register TagPrefix="pmode" TagName="persmode" Src="~/persMode.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">
        <!-- Create Web Part manager and zone for the color user control. -->
        <asp:WebPartManager ID="WebPartManager1" runat="server"></asp:WebPartManager>
            <asp:WebPartZone ID="WebPartZone1" runat="server" HeaderText="Color Change Zone">
                <ZoneTemplate>
                <!-- Note that the control is Shared since it is declared on the page. -->
                    <control:colorcontrol id="color1" title="Color Control" runat="server" />
                </ZoneTemplate>
            </asp:WebPartZone>
        
        <br />
            <!-- Create Web Part zone for the personalization mode user control. -->
            <asp:WebPartZone ID="WebPartZone2" runat="server" HeaderText="Scope Change Zone" Height="109px">
                <ZoneTemplate>
                    <pmode:persmode  ID="Persmode1" runat="server" title="Scope Tool"/>
                </ZoneTemplate>
            </asp:WebPartZone>
   
    </form>
</body>
</html>
<%@ Page Language="C#" %>
<%@ Register TagPrefix="control"  TagName="colorcontrol" Src="~/color.ascx"%>
<%@Register TagPrefix="pmode" TagName="persmode" Src="~/persMode.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">
        <!-- Create Web Part manager and zone for the color user control. -->
        <asp:WebPartManager ID="WebPartManager1" runat="server"></asp:WebPartManager>
            <asp:WebPartZone ID="WebPartZone1" runat="server" HeaderText="Color Change Zone">
                <ZoneTemplate>
                <!-- Note that the control is Shared since it is declared on the page. -->
                    <control:colorcontrol id="color1" title="Color Control" runat="server" />
                </ZoneTemplate>
            </asp:WebPartZone>
        
        <br />
            <!-- Create Web Part zone for the personalization mode user control. -->
            <asp:WebPartZone ID="WebPartZone2" runat="server" HeaderText="Scope Change Zone" Height="109px">
                <ZoneTemplate>
                    <pmode:persmode  ID="Persmode1" runat="server" title="Scope Tool"/>
                </ZoneTemplate>
            </asp:WebPartZone>
   
    </form>
</body>
</html>

下列範例程式代碼會建立登入頁面。 成功登入之後,它會重新導向至主要 aspx 頁面。

重要

這個範例有一個可接受使用者輸入的文字方塊,這可能會造成安全性威脅。 根據預設,ASP.NET Web 網頁會驗證使用者輸入未包含指令碼或 HTML 項目。 如需詳細資訊,請參閱 Script Exploits Overview (指令碼攻擊概觀)。

<%@ 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">

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Login ID="Login1" runat="server" BorderWidth="1px" BorderColor="#E6E2D8" BorderPadding="4"
            BorderStyle="Solid" BackColor="#F7F6F3" ForeColor="#333333" Font-Names="Verdana"
            Font-Size="0.8em" DestinationPageUrl="~/Defaultcs.aspx">
            <InstructionTextStyle ForeColor="Black" Font-Italic="True" Font-Size="0.8em" />
            <LoginButtonStyle Font-Names="Verdana" Font-Size="0.8em" BorderStyle="Solid" BorderWidth="1px"
                BorderColor="#CCCCCC" BackColor="#FFFBFF" ForeColor="#284775" />
            <TextBoxStyle Font-Size="0.8em" />
            <LabelStyle Font-Size="0.8em" />
            <TitleTextStyle ForeColor="White" Font-Size="0.9em" Font-Bold="True" BackColor="#5D7B9D" />
            <HyperLinkStyle Font-Size="0.8em" />
            <CheckBoxStyle Font-Size="0.8em" />
            <FailureTextStyle ForeColor="#FF0000" Font-Size="0.8em" />
        </asp:Login>   
You can create new users with the ASP.NET Web Site Administration Tool in Microsoft Visual Studio 2005. 
See also the web.config file for user authorization examples.
    </div>
    </form>
</body>
</html>
<%@ 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">

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Login ID="Login1" runat="server" BorderWidth="1px" BorderColor="#E6E2D8" BorderPadding="4"
            BorderStyle="Solid" BackColor="#F7F6F3" ForeColor="#333333" Font-Names="Verdana"
            Font-Size="0.8em" DestinationPageUrl="~/Defaultvb.aspx">
            <InstructionTextStyle ForeColor="Black" Font-Italic="True" Font-Size="0.8em" />
            <LoginButtonStyle Font-Names="Verdana" Font-Size="0.8em" BorderStyle="Solid" BorderWidth="1px"
                BorderColor="#CCCCCC" BackColor="#FFFBFF" ForeColor="#284775" />
            <TextBoxStyle Font-Size="0.8em" />
            <LabelStyle Font-Size="0.8em" />
            <TitleTextStyle ForeColor="White" Font-Size="0.9em" Font-Bold="True" BackColor="#5D7B9D" />
            <HyperLinkStyle Font-Size="0.8em" />
            <CheckBoxStyle Font-Size="0.8em" />
            <FailureTextStyle ForeColor="#FF0000" Font-Size="0.8em" />
        </asp:Login>   
You can create new users with the ASP.NET Web Site Administration Tool in Microsoft Visual Studio 2005. 
See also the web.config file for user authorization examples.
    </div>
    </form>
</body>
</html>

下列範例程式代碼是此應用程式 Web.config 檔案的一部分。 本節說明如何設定用戶的授權,在此案例中為 “user2”,以輸入共用個人化範圍,以及修改個人化狀態資訊。 它也會顯示角色的範例,在此案例中為「系統管理員」,可用來允許「系統管理員」角色中的使用者進入共用個人化範圍,以及修改個人化狀態資訊。

<webParts>
    <personalization defaultProvider="AspNetSqlPersonalizationProvider">
        <authorization>
            <allow users="user2" verbs="enterSharedScope, modifyState"/>
            <allow roles="admin" verbs="enterSharedScope, modifyState"/>
        </authorization>
    </personalization>
</webParts>
<webParts>
    <personalization defaultProvider="AspNetSqlPersonalizationProvider">
        <authorization>
            <allow users="user2" verbs="enterSharedScope, modifyState"/>
            <allow roles="admin" verbs="enterSharedScope, modifyState"/>
        </authorization>
    </personalization>
</webParts>

下列範例程式代碼會根據目前的個人化範圍和使用者的授權許可權,建立名為 Color.ascx的使用者控件,讓使用者變更個人化屬性,在此案例中為兩個文本框的背景色彩。

重要

這個範例有一個可接受使用者輸入的文字方塊,這可能會造成安全性威脅。 根據預設,ASP.NET Web 網頁會驗證使用者輸入未包含指令碼或 HTML 項目。 如需詳細資訊,請參閱 Script Exploits Overview (指令碼攻擊概觀)。

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

<script runat="server">
    // User a field to reference the current WebPartManager.
    private WebPartManager _manager;
    
    //  Defines personalized property for User scope. In this case, the property is
    //   the background color of the text box.
    [Personalizable(PersonalizationScope.User)]
    public System.Drawing.Color UserColorChoice
    {
        get
        {
            return _coloruserTextBox.BackColor;
        }
        set
        {
            _coloruserTextBox.BackColor = value;
        }
    }

    // Defines personalized property for Shared scope. In this case, the property is
    //   the background color of the text box.
    [Personalizable(PersonalizationScope.Shared) ]
    public System.Drawing.Color SharedColorChoice
    {
        get
        {
            return _colorsharedTextBox.BackColor;
        }
        set
        {
            _colorsharedTextBox.BackColor = value;
        }
    }


    void Page_Init(object sender, EventArgs e)
    {
       _manager = WebPartManager.GetCurrentWebPartManager(Page);       
    }

    protected void Page_Load(object src, EventArgs e) 
    {
     // If Web Parts manager scope is User, hide the button that changes shared control.
       if (_manager.Personalization.Scope == PersonalizationScope.User)
       {
           _sharedchangeButton.Visible = false;
                  if (!_manager.Personalization.IsModifiable)
                      _userchangeButton.Enabled = false;
       }
       else
       {
           _sharedchangeButton.Visible = true; 
                  if (!_manager.Personalization.IsModifiable)
                    {
                      _sharedchangeButton.Enabled = false;
                      _userchangeButton.Enabled = false;
                    }
       } 
    }
    
    // Changes color of the User text box background when button clicked by authorized user.
    protected void _userButton_Click(object src, EventArgs e)
    {
        switch(_coloruserTextBox.BackColor.Name)
        {
            case "Red":
                _coloruserTextBox.BackColor = System.Drawing.Color.Yellow;
                break;
            case "Yellow":
                _coloruserTextBox.BackColor = System.Drawing.Color.Green;
                break;
            case "Green":
                _coloruserTextBox.BackColor = System.Drawing.Color.Red;
                break;
        }
    }
        
    // Changes color of the Shared text box background when button clicked by authorized user.
    protected void _sharedButton_Click(object src, EventArgs e)
    {
        switch (_colorsharedTextBox.BackColor.Name)
        {
            case "Red":
                _colorsharedTextBox.BackColor = System.Drawing.Color.Yellow;
                break;
            case "Yellow":
                _colorsharedTextBox.BackColor = System.Drawing.Color.Green;
                break;
            case "Green":
                _colorsharedTextBox.BackColor = System.Drawing.Color.Red;
                break;
        }        
    }   

</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>WebParts Personalization Example</title>
</head>
<body>
<p>
    <asp:LoginName ID="LoginName1" runat="server" BorderWidth="500" BorderStyle="none" />
    <asp:LoginStatus ID="LoginStatus1" LogoutAction="RedirectToLoginPage" runat="server" />
</p>
    <asp:Label ID="ScopeLabel" Text="Scoped Properties:" runat="server" Width="289px"></asp:Label>
    <br />
    <table style="width: 226px">
        
        <tr>
            <td>
                <asp:TextBox ID="_coloruserTextBox" Font-Bold="True" Height="110px" 
                runat="server" Text="User Property" BackColor="red" Width="110px" /> 
            </td>
            <td>       
                <asp:TextBox ID="_colorsharedTextBox" runat="server" Height="110px" 
                Width="110px" Text="Shared Property" BackColor="red" Font-Bold="true" />
            </td>
       </tr>
        <tr>
            <td>
                <asp:Button Text="Change User Color" ID="_userchangeButton" 
                runat="server" OnClick="_userButton_Click" />
            </td>
            <td >
                <asp:Button Text="Change Shared Color" ID="_sharedchangeButton" 
                runat="server" OnClick="_sharedButton_Click" />
            </td>
        </tr>
    </table>
</body>
</html>
<%@ Control Language="VB" %>

<script runat="server">
     ' User a field to reference the current WebPartManager.
Private _manager As WebPartManager

'  Defines personalized property for User scope. In this case, the property is
'   the background color of the text box.

<Personalizable(PersonalizationScope.User)>  _
Public Property UserColorChoice() As System.Drawing.Color 
    Get
        Return _coloruserTextBox.BackColor
    End Get
    Set
        _coloruserTextBox.BackColor = value
    End Set
End Property

' Defines personalized property for Shared scope. In this case, the property is
'   the background color of the text box.

<Personalizable(PersonalizationScope.Shared)>  _
Public Property SharedColorChoice() As System.Drawing.Color 
    Get
        Return _colorsharedTextBox.BackColor
    End Get
    Set
        _colorsharedTextBox.BackColor = value
    End Set
End Property



Sub Page_Init(ByVal sender As Object, ByVal e As EventArgs) 
    _manager = WebPartManager.GetCurrentWebPartManager(Page)

End Sub 'Page_Init


Protected Sub Page_Load(ByVal src As Object, ByVal e As EventArgs) 
    ' If Web Parts manager scope is User, hide the button that changes shared control.
    If _manager.Personalization.Scope = PersonalizationScope.User Then
        _sharedchangeButton.Visible = False
        If Not _manager.Personalization.IsModifiable Then
            _userchangeButton.Enabled = False
        End If
    Else
        _sharedchangeButton.Visible = True
        If Not _manager.Personalization.IsModifiable Then
            _sharedchangeButton.Enabled = False
            _userchangeButton.Enabled = False
        End If
    End If

End Sub 'Page_Load
 
' Changes color of the User text box background when button clicked by authorized user.
Protected Sub _userButton_Click(ByVal src As Object, ByVal e As EventArgs) 
    Select Case _coloruserTextBox.BackColor.Name
        Case "Red"
            _coloruserTextBox.BackColor = System.Drawing.Color.Yellow
        Case "Yellow"
            _coloruserTextBox.BackColor = System.Drawing.Color.Green
        Case "Green"
            _coloruserTextBox.BackColor = System.Drawing.Color.Red
    End Select

End Sub '_userButton_Click


' Changes color of the Shared text box background when button clicked by authorized user.
Protected Sub _sharedButton_Click(ByVal src As Object, ByVal e As EventArgs) 
    Select Case _colorsharedTextBox.BackColor.Name
        Case "Red"
            _colorsharedTextBox.BackColor = System.Drawing.Color.Yellow
        Case "Yellow"
            _colorsharedTextBox.BackColor = System.Drawing.Color.Green
        Case "Green"
            _colorsharedTextBox.BackColor = System.Drawing.Color.Red
    End Select

End Sub '_sharedButton_Click

</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>WebParts Personalization Example</title>
</head>
<body>
<p>
    <asp:LoginName ID="LoginName1" runat="server" BorderWidth="500" BorderStyle="none" />
    <asp:LoginStatus ID="LoginStatus1" LogoutAction="RedirectToLoginPage" runat="server" />
</p>
    <asp:Label ID="ScopeLabel" Text="Scoped Properties:" runat="server" Width="289px"></asp:Label>
    <br />
    <table style="width: 226px">
        
        <tr>
            <td>
                <asp:TextBox ID="_coloruserTextBox" Font-Bold="True" Height="110px" 
                runat="server" Text="User Property" BackColor="red" Width="110px" /> 
            </td>
            <td>       
                <asp:TextBox ID="_colorsharedTextBox" runat="server" Height="110px" 
                Width="110px" Text="Shared Property" BackColor="red" Font-Bold="true" />
            </td>
       </tr>
        <tr>
            <td>
                <asp:Button Text="Change User Color" ID="_userchangeButton" 
                runat="server" OnClick="_userButton_Click" />
            </td>
            <td >
                <asp:Button Text="Change Shared Color" ID="_sharedchangeButton" 
                runat="server" OnClick="_sharedButton_Click" />
            </td>
        </tr>
    </table>
</body>
</html>

下列範例程式代碼會建立名為 Persmode.ascx的使用者控件,以顯示目前的個人化範圍和修改狀態的用戶權力,以及輸入共用範圍。 它也具有重設按鈕來重設目前的個人化狀態。

<%@ control language="C#" %>

<script runat="server">
  
 // Use a field to reference the current WebPartManager.
  private WebPartManager _manager;

    protected void Page_Load(object src, EventArgs e)
    {
        // Get the current Web Parts manager.
        _manager = WebPartManager.GetCurrentWebPartManager(Page);
        
        // All radio buttons are disabled; the button settings show what the current state is.
        EnterSharedRadioButton.Enabled = false;
        ModifyStateRadioButton.Enabled = false;

        // If Web Parts manager is in User scope, set scope button.
        if (_manager.Personalization.Scope == PersonalizationScope.User)
            UserScopeRadioButton.Checked = true;
        else
            SharedScopeRadioButton.Checked = true;

        // Based on current user rights to enter Shared scope, set buttons.
        if (_manager.Personalization.CanEnterSharedScope)
        {
            EnterSharedRadioButton.Checked = true;
            No_Shared_Scope_Label.Visible = false;
            Toggle_Scope_Button.Enabled = true;
        }
        else
        {
            EnterSharedRadioButton.Checked = false;
            No_Shared_Scope_Label.Visible = true;
            Toggle_Scope_Button.Enabled = false;
        }

        // Based on current user rights to modify personalization state, set buttons.
        if (_manager.Personalization.IsModifiable)
        {
            ModifyStateRadioButton.Checked = true;
            Reset_User_Button.Enabled = true;
        }
        else
        {
            ModifyStateRadioButton.Checked = false;
            Reset_User_Button.Enabled = false;
        }
    }
    // <snippet6>
  // Resets all of a user and shared personalization data for the page.
    protected void Reset_CurrentState_Button_Click(object src, EventArgs e)
    {
        // User must be authorized to modify state before a reset can occur.
        //When in user scope, all users by default can change their own data.
        if (_manager.Personalization.IsModifiable)
        {
            _manager.Personalization.ResetPersonalizationState();
        }
    }
  //  </snippet6>

    // <snippet7>
    // Allows authorized user to change personalization scope.
    protected void Toggle_Scope_Button_Click(object sender, EventArgs e)
    {
        if (_manager.Personalization.CanEnterSharedScope)
        {
            _manager.Personalization.ToggleScope();
        }
        
    }
 // </snippet7>
</script>
<div>
    &nbsp;<asp:Panel ID="Panel1" runat="server" 
    Borderwidth="1" 
    Width="208px" 
    BackColor="lightgray"
    Font-Names="Verdana, Arial, Sans Serif" Height="214px" >
    <asp:Label ID="Label1" runat="server" 
      Text="Page Scope" 
      Font-Bold="True"
      Font-Size="8pt"
      Width="120px" />&nbsp;<br />
    
    
      <asp:RadioButton ID="UserScopeRadioButton" runat="server" 
        Text="User" 
        AutoPostBack="true"
        GroupName="Scope"  
         Enabled="false" />
      <asp:RadioButton ID="SharedScopeRadioButton" runat="server" 
        Text="Shared" 
        AutoPostBack="true"
        GroupName="Scope" 
        Enabled="false" />
        <br />
        <asp:Label BorderStyle="None" Font-Bold="True" Font-Names="Courier New" ID="No_Shared_Scope_Label" Font-Size="Smaller" ForeColor="red"
           runat="server" Visible="false" Width="179px">User cannot enter Shared scope</asp:Label>
        <br />
        <asp:Label ID="Label2" runat="server" 
      Text="Current User Can:" 
      Font-Bold="True"
      Font-Size="8pt"
      Width="165px" />
      <br />
        <asp:RadioButton ID="ModifyStateRadioButton" runat="server" 
        Text="Modify State" Width="138px" />
        <br />
        <asp:RadioButton ID="EnterSharedRadioButton" runat="server" 
        Text="Enter Shared Scope" 
        AutoPostBack="true"  />&nbsp;<br />
        <br />
        <asp:Button ID="Toggle_Scope_Button" OnClick="Toggle_Scope_Button_Click" runat="server"
            Text="Change Scope" Width="186px" /><br />
        <br />
        <asp:Button ID="Reset_User_Button" OnClick="Reset_CurrentState_Button_Click" runat="server"
            Text="Reset Current Personalization" Width="185px" /></asp:Panel>
    &nbsp; &nbsp;
</div>
<%@ control language="VB" %>

<script runat="server">
  
  ' Use a field to reference the current WebPartManager.
Private _manager As WebPartManager


Protected Sub Page_Load(ByVal src As Object, ByVal e As EventArgs) 
    ' Get the current Web Parts manager.
    _manager = WebPartManager.GetCurrentWebPartManager(Page)
    
    ' All radio buttons are disabled; the button settings show what the current state is.
    EnterSharedRadioButton.Enabled = False
    ModifyStateRadioButton.Enabled = False
    
    ' If Web Parts manager is in User scope, set scope button.
    If _manager.Personalization.Scope = PersonalizationScope.User Then
        UserScopeRadioButton.Checked = True
    Else
        SharedScopeRadioButton.Checked = True
    End If 
    ' Based on current user rights to enter Shared scope, set buttons.
    If _manager.Personalization.CanEnterSharedScope Then
        EnterSharedRadioButton.Checked = True
        No_Shared_Scope_Label.Visible = False
        Toggle_Scope_Button.Enabled = True
    Else
        EnterSharedRadioButton.Checked = False
        No_Shared_Scope_Label.Visible = True
        Toggle_Scope_Button.Enabled = False
    End If
    
    ' Based on current user rights to modify personalization state, set buttons.
    If _manager.Personalization.IsModifiable Then
        ModifyStateRadioButton.Checked = True
        Reset_User_Button.Enabled = True
    Else
        ModifyStateRadioButton.Checked = False
        Reset_User_Button.Enabled = False
    End If

End Sub 'Page_Load

' <snippet6>
' Resets all of a user and shared personalization data for the page.
Protected Sub Reset_CurrentState_Button_Click(ByVal src As Object, ByVal e As EventArgs) 
    ' User must be authorized to modify state before a reset can occur.
    'When in user scope, all users by default can change their own data.
    If _manager.Personalization.IsModifiable Then
        _manager.Personalization.ResetPersonalizationState()
    End If

End Sub 'Reset_CurrentState_Button_Click

'  </snippet6>
' <snippet7>
' Allows authorized user to change personalization scope.
Protected Sub Toggle_Scope_Button_Click(ByVal sender As Object, ByVal e As EventArgs) 
    If _manager.Personalization.CanEnterSharedScope Then
        _manager.Personalization.ToggleScope()
    End If

End Sub 'Toggle_Scope_Button_Click 
' </snippet7>
</script>
<div>
    &nbsp;<asp:Panel ID="Panel1" runat="server" 
    Borderwidth="1" 
    Width="208px" 
    BackColor="lightgray"
    Font-Names="Verdana, Arial, Sans Serif" Height="214px" >
    <asp:Label ID="Label1" runat="server" 
      Text="Page Scope" 
      Font-Bold="True"
      Font-Size="8pt"
      Width="120px" />&nbsp;<br />
    
    
      <asp:RadioButton ID="UserScopeRadioButton" runat="server" 
        Text="User" 
        AutoPostBack="true"
        GroupName="Scope"  
         Enabled="false" />
      <asp:RadioButton ID="SharedScopeRadioButton" runat="server" 
        Text="Shared" 
        AutoPostBack="true"
        GroupName="Scope" 
        Enabled="false" />
        <br />
        <asp:Label BorderStyle="None" Font-Bold="True" Font-Names="Courier New" ID="No_Shared_Scope_Label" Font-Size="Smaller" ForeColor="red"
           runat="server" Visible="false" Width="179px">User cannot enter Shared scope</asp:Label>
        <br />
        <asp:Label ID="Label2" runat="server" 
      Text="Current User Can:" 
      Font-Bold="True"
      Font-Size="8pt"
      Width="165px" />
      <br />
        <asp:RadioButton ID="ModifyStateRadioButton" runat="server" 
        Text="Modify State" Width="138px" />
        <br />
        <asp:RadioButton ID="EnterSharedRadioButton" runat="server" 
        Text="Enter Shared Scope" 
        AutoPostBack="true"  />&nbsp;<br />
        <br />
        <asp:Button ID="Toggle_Scope_Button" OnClick="Toggle_Scope_Button_Click" runat="server"
            Text="Change Scope" Width="186px" /><br />
        <br />
        <asp:Button ID="Reset_User_Button" OnClick="Reset_CurrentState_Button_Click" runat="server"
            Text="Reset Current Personalization" Width="185px" /></asp:Panel>
    &nbsp; &nbsp;
</div>

備註

這個類別會實作執行較低層級個人化作業所需的邏輯。 雖然 類別 WebPartManager 會管理個人化的高階生命週期,但它是 WebPartPersonalization 負責實際實作執行特定個人化動作所需實體步驟的類別。 接著,類別 WebPartPersonalization 會依賴的 PersonalizationProvider 實作來與基礎數據存放區通訊,以取得個人化資訊。

如果您使用的預設實 WebPartManager作,它會建立類別的 WebPartPersonalization 實例,您可以藉由參考 Personalization 屬性來使用。 例如,若要存取 InitialScope 屬性,您可以指定 WebPartManager.Personalization.InitialScope

給繼承者的注意事項

類別 WebPartPersonalization 與控件和個人化基礎結構的其餘部分緊密搭配 WebPartManager 運作。 個人化的默認實作是非常健全的子系統,應符合您的個人化需求。 在大部分情況下,如果您想要自定義個人化,您可以建立數據提供者,藉由建立繼承自 PersonalizationProvider的類別,以作為個人化提供者使用。

如果您想要建立個人化子系統與 和 類別所提供的WebPartPersonalization子系統明顯不同,您應該衍生自 WebPartPersonalization 並新增您自己的自定義邏輯來建立自定義WebPartPersonalizationWebPartManager作。 然後,藉由衍生自 WebPartManager來建立自定義WebPartManager實作,新增您自己的自定義邏輯,並覆寫 CreatePersonalization() 方法以傳回您的自定義WebPartPersonalization實作。 WebPartManager因為控件會透過 WebPartPersonalization 實例對個人化基礎結構提出要求,所以WebPartManager控件不會直接與實作的參考互動或保留參考PersonalizationProvider

建構函式

WebPartPersonalization(WebPartManager)

初始化 WebPartPersonalization 類別的新執行個體。

欄位

EnterSharedScopeUserCapability

表示進入 WebPartUserCapability 範圍之使用者授權的 Shared 執行個體。

ModifyStateUserCapability

表示修改個人化狀態之使用者授權的 WebPartUserCapability 執行個體。

屬性

CanEnterSharedScope

傳回數值,指出使用者是否獲得授權進入 Shared 範圍。

Enabled

傳回數值,指出是否要求啟用相關聯 WebPartManager 控制項的個人化。

HasPersonalizationState

傳回數值,指出目前的網頁和個人化範圍是否有相關聯的個人化資料。

InitialScope

取得或設定預設個人化範圍。

IsEnabled

取得值,指出是否已啟用個人化,並成功載入 WebPartPersonalization 類別此執行個體的個人化資料。

IsInitialized

取得值,指出是否已啟用個人化,並成功載入 WebPartPersonalization 類別此執行個體的個人化資料。

IsModifiable

取得值,指出目前使用者是否獲得授權修改狀態資訊。

ProviderName

取得或設定個人化的提供者名稱。

Scope

取得父 WebPartManager 控制項的目前個人化範圍。

ShouldResetPersonalizationState

取得或設定值,指出是否已重設目前網頁的個人化資料 (例如,要求刪除基礎資料存放區中的個人化資料)。

UserCapabilities

從授與目前使用者的 WebPartUserCapability 取得使用者能力集合。

WebPartManager

取得與這個 WebPartManager 執行個體相關聯之目前父 WebPartPersonalization 控制項的執行個體。

方法

ApplyPersonalizationState()

將個人化資料套用至父 WebPartManager 控制項。

ApplyPersonalizationState(WebPart)

WebPart 控制項要求時,將個人化資料套用至指定的 WebPartManager 控制項。

ChangeScope(PersonalizationScope)

將目前網頁的 PersonalizationScope 執行個體變更為指定範圍。

CopyPersonalizationState(WebPart, WebPart)

擷取一個 Web 組件控制項的個人化狀態,並將其套用至第二個 Web 組件控制項。

EnsureEnabled(Boolean)

確認 WebPartPersonalization 執行個體已完成初始化,或目前使用者有權修改個人化狀態。

Equals(Object)

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

(繼承來源 Object)
ExtractPersonalizationState()

擷取父 WebPartManager 控制項的個人化資料。

ExtractPersonalizationState(WebPart)

WebPartManager 控制項要求時,擷取 Web 組件控制項的個人化資料。

GetAuthorizationFilter(String)

擷取與指定 Web 組件控制項相關聯的授權篩選 (如果有的話)。

GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
Load()

初始化個人化。

MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ResetPersonalizationState()

重設基礎資料存放區中目前網頁、範圍和使用者的個人化資料。

Save()

將目前網頁、範圍和使用者的個人化資料儲存至基礎資料存放區。

SetDirty()

將父 WebPartManager 控制項標記為有已變更的個人化資料 ("Dirty")。

SetDirty(WebPart)

將指定的 Web 組件控制項標記為有已變更的個人化資料 ("Dirty")。

ToggleScope()

將目前網頁的個人化範圍從 User 切換至 Shared,或從 Shared 切換至 User

ToString()

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

(繼承來源 Object)

適用於

另請參閱