Overview of Personalization


When a user modifies the layout or content of a page that contains Web Parts, the personalization changes are scoped to two things: the page and the user. Each page in an application can have different personalization data associated with it. If you change the name of a page, all personalization data that was associated with the page is lost and you must personalize the page again from scratch.

Note

Personalization data is scoped to a page's application relative path. If you move a Web Part application from one server to another, personalization data is not lost. However, if you rename an application, the personalization data is lost.


Personalization is also scoped to the user. The Web Part Framework supports two types of personalization: User and Shared. By default, when a user customizes a Web Part page, the changes have User scope. Each user of a Web Part application can personalize the same application in different ways.

You can also make Shared scoped hanges to a Web Part application. A Shared scope change, unlike a User scope change, has an effect on all users of a Web Part application. Typically, you want to enable only a select group of administrators to make Shared scope changes to an application.

Shared and User scoped personalization data is merged when a user requests a page. For example, an administrator can add a standard set of Web Parts to a page. A particular user can add an additional set of Web Parts. When the page is displayed, both sets of Web Parts are displayed.

So how is all this personalization data stored? The Web Part framework uses the provider model, so it is really up to you. Later in this chapter, we'll extend the ASP.NET Framework with our own custom personalization providers.

The default and only personalization provider included in the ASP.NET framework is the SqlPersonalizationProvider. This provider stores personalization data in two database tables: aspnet_PersonalizationAllUsers and aspnet_PersonalizationPerUser. The first table contains the personalization data that is scoped to all users, and the second table contains the personalization data that is scoped to a particular user. By default, these database tables are located in the ASPNETDB.mdf SQL Server 2005 Express database, located in your application's APP_Data folder.

Using the WebPartPersonalization Class

Under the covers, the WebPartPersonalization class is responsible for all of the low-level operations related to personalization. This class acts as a bridge between the WebPartManager control and a particular personalization provider.

The WebPartPersonalization class is exposed by the WebPartManager control's Personalization property. You access the properties and methods of the WebPartPersonalization class through the WebPartManager control.

The WebPartPersonalization class has a number of useful properties:

  • CanEnterSharedScope Use this property to determine whether the current user can make changes to the page that have Shared personalization scope (has an effect on all users).

  • Enabled Use this property to disable personalization for the current page.

  • HasPersonalizationState Use this property to determine whether any personalization data is associated with the current page and the current user, given the current personalization scope.

  • InitialScope Use this property to place a page in either Shared or User personalization scope.

  • IsEnabled Use this property to determine whether personalization is currently enabled for this page.

  • IsModified Use this property to determine whether the current user can make changes to personalization data.

  • ProviderName Use this property to retrieve or set the name of the personalization provider.

  • Scope Use this property to determine the current personalization scope (User or Shared).

Furthermore, the WebPartPersonalization class has two important methods:

  • ResetPersonalizationState Deletes personalization data associated with the current page and the current user, given the current personalization scope.

  • ToggleScope Toggles the current personalization scope between User personalization scope and Shared personalization scope (or vice versa).

Creating a Personalization Manager

When working with personalization, it helps to see exactly how personalization data is being stored. In the examples in this section, we take advantage of the properties and methods of the WebPartPersonalization class to create a Personalization Manager. The Personalization Manager is contained in Listing 29.1.

Listing 29.1. PersonalizationManager.ascx

[View full width]

<%@ Control Language="VB" ClassName="PersonalizationManager" %> <script runat="server">     ''' <summary>     ''' Display Personalization Information     ''' </summary>     Private Sub Page_PreRender()         Dim wpm As WebPartManager = WebPartManager.GetCurrentWebPartManager(Page)         lblCurrentScope.Text = wpm.Personalization.Scope.ToString()         lblIsModifiable.Text = wpm.Personalization.IsModifiable.ToString()         lblCanEnterSharedScope.Text = wpm.Personalization.CanEnterSharedScope.ToString()         lblHasPersonalizationState.Text =  wpm.Personalization.HasPersonalizationState .ToString()         lnkToggleScope.Visible = wpm.Personalization.CanEnterSharedScope     End Sub     ''' <summary>     ''' Switches to Shared Scope     ''' </summary>     Protected Sub lnkToggleScope_Click(ByVal sender As Object, ByVal e As EventArgs)         Dim wpm As WebPartManager = WebPartManager.GetCurrentWebPartManager(Page)         wpm.Personalization.ToggleScope()     End Sub     ''' <summary>     ''' Deletes Personalization data     ''' </summary>     Protected Sub lnkReset_Click(ByVal sender As Object, ByVal e As EventArgs)         Dim wpm As WebPartManager = WebPartManager.GetCurrentWebPartManager(Page)         wpm.Personalization.ResetPersonalizationState()     End Sub </script> <div > Current Scope: <asp:Label          Runat="server" /> Can Modify State: <asp:Label          Runat="server" /> Can Enter Shared Scope: <asp:Label          Runat="server" /> Has Personalization State: <asp:Label          Runat="server" /> <span> <asp:LinkButton          Text="Toggle Scope"     OnClick="lnkToggleScope_Click"     Runat="server" /> </span> <asp:LinkButton          Text="Reset Personalization"     Runat="server" OnClick="lnkReset_Click" /> </div> 

The Personalization Manager displays an information bar across the top of a page (see Figure 29.1). The bar displays the values of the following properties:

  • Current Scope Displays whether the page is in User or Shared scope personalization mode.

  • Can Modify State Displays whether the user can modify the personalization state associated with the page.

  • Can Enter Shared Scope Displays whether the user can enter Shared Personalization scope.

  • Has Personalization State Displays whether the current page, given the current personalization scope, has personalization data associated with it.

Figure 29.1. Viewing the Personalization Manager.


The Personalization Manager includes links to invoke the following methods:

  • Toggle Scope Clicking this link switches the page from User to Shared personalization scope (or back again). This link appears only when the user can enter Shared personalization scope.

  • Reset Personalization Clicking this link removes the personalization data associated with the current personalization scope. Clicking this link in User scope personalization mode removes all User scoped personalization data, and clicking the link in Shared scope personalization mode removes all Shared scoped personalization data.

You can use the Personalization Manager in any Web Part page. For example, you can experiment with the Personalization Manager with the page in Listing 29.2.

Listing 29.2. ShowPersonalizationManager.aspx

<%@ Page Language="VB" %> <%@ Register TagPrefix="user" TagName="PersonalizationManager"     src="/books/3/444/1/html/2/~/PersonalizationManager.ascx" %> <%@ Register TagPrefix="user" TagName="FirstSimplePart"     src="/books/3/444/1/html/2/~/FirstSimplePart.ascx" %> <%@ Register TagPrefix="user" TagName="SecondSimplePart"     src="/books/3/444/1/html/2/~/SecondSimplePart.ascx" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"    "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <script runat="server">     Protected  Sub Menu1_MenuItemClick(ByVal sender As Object, ByVal e As MenuEventArgs)         WebPartManager1.DisplayMode = WebPartManager1.DisplayModes(e.Item.Text)     End Sub </script> <html xmlns="http://www.w3.org/1999/xhtml" > <head  runat="server">     <style type="text/css">         .personalizationManager         {             border:dotted 2px orange;             padding:5px;             background-color:White;             font:12px Arial, Sans-Serif;         }         .personalizationManager span         {             padding-right:10px;             margin-right:10px;             border-right:solid 1px black;         }         .personalizationManager a         {             text-decoration:none;         }         .column         {             float:left;             width:40%;             height:200px;             margin-right:10px;             border:solid 1px black;             background-color: white;         }         .menu         {             margin:5px 0px;         }         html         {             background-color:#eeeeee;         }     </style>     <title>Show Personalization Manager</title> </head> <body>     <form  runat="server">     <user:PersonalizationManager                  Runat="Server" />     <asp:WebPartManager                  Runat="server" />         <asp:Menu                          OnMenuItemClick="Menu1_MenuItemClick"             Orientation="Horizontal"             Css             Runat="server">             <Items>             <asp:MenuItem Text="Browse" />             <asp:MenuItem Text="Design" />             </Items>         </asp:Menu>         <asp:WebPartZone                          Css             Runat="server">             <ZoneTemplate>             <user:FirstSimplePart                                  Title="First Web Part"                 Description="Our first simple Web Part"                 Runat="server" />             <user:SecondSimplePart                                  Title="Second Web Part"                 Description="Our second simple Web Part"                 Runat="server" />             </ZoneTemplate>         </asp:WebPartZone>         <asp:WebPartZone                          Css             Runat="server" />     </form> </body> </html> 

When you first open the page in Listing 29.2, the Personalization Manager displays the fact that no personalization data is associated with the page. If you click the Design link, and re-arrange the Web Parts in the page, you create new personalization data. You can click the Reset Personalization link at any time to clear away any personalization data associated with the page.




ASP. NET 2.0 Unleashed
ASP.NET 2.0 Unleashed
ISBN: 0672328232
EAN: 2147483647
Year: 2006
Pages: 276

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net