แก้ไข

แชร์ผ่าน


WebPartManager.IPersonalizable.IsDirty Property

Definition

Gets a value that indicates whether custom personalization state data managed by the WebPartManager control has changed on a Web page.

property bool System::Web::UI::WebControls::WebParts::IPersonalizable::IsDirty { bool get(); };
bool System.Web.UI.WebControls.WebParts.IPersonalizable.IsDirty { get; }
member this.System.Web.UI.WebControls.WebParts.IPersonalizable.IsDirty : bool
 ReadOnly Property IsDirty As Boolean Implements IPersonalizable.IsDirty

Property Value

A Boolean value that indicates whether the personalization state data has changed.

Implements

Examples

The following code example demonstrates a simple usage of the IPersonalizable.IsDirty property, to indicate some common page personalization instances that cause a WebPartManager control's personalization data to change.

The code example has four parts:

  • A user control that allows you to change display modes on a page that contains Web Parts controls.

  • A source file that contains the code for two custom WebPart controls that can be connected, and an interface.

  • A Web page that hosts all the controls.

  • An explanation of how the code example works.

The first part of the code example is the user control for changing display modes. You can obtain the source code for the user control from the Example section of the WebPartManager class overview. For information about display modes and how the user control works, see Walkthrough: Changing Display Modes on a Web Parts Page.

The second part of the example is the source file with the custom controls and the interface. Notice that the IZipCode interface exposes one method, and that this method as implemented in the custom ZipCodeWebPart control serves as a callback method to enable ZipCodeWebPart to act as a provider in a connection scenario. The other control, WeatherWebPart, acts as the consumer control in a connection; it can consume the particular interface provided by ZipCodeWebPart. In a real application, WeatherWebPart could consume a personalized ZIP Code value from the provider, and then provide graphical weather information to users.

For the code example to run, you must compile this source code. You can compile it explicitly and put the resulting assembly in your Web site's Bin folder or the global assembly cache. Alternatively, you can put the source code in your site's App_Code folder, where it will be dynamically compiled at run time. This code example uses dynamic compilation; therefore, notice that the Register directive for this component at the top of the Web page contains only TagPrefix and Namespace attributes, without an Assembly attribute. For a walkthrough that demonstrates how to compile, see Walkthrough: Developing and Using a Custom Web Server Control.

namespace Samples.AspNet.CS.Controls
{
  using System;
  using System.Web;
  using System.Web.Security;
  using System.Security.Permissions;
  using System.Web.UI;
  using System.Web.UI.WebControls;
  using System.Web.UI.WebControls.WebParts;

  [AspNetHostingPermission(SecurityAction.Demand,
    Level = AspNetHostingPermissionLevel.Minimal)]
  [AspNetHostingPermission(SecurityAction.InheritanceDemand,
    Level = AspNetHostingPermissionLevel.Minimal)]
  public interface IZipCode
  {
    string ZipCode { get; set;}
  }

  [AspNetHostingPermission(SecurityAction.Demand,
    Level = AspNetHostingPermissionLevel.Minimal)]
  [AspNetHostingPermission(SecurityAction.InheritanceDemand,
    Level = AspNetHostingPermissionLevel.Minimal)]
  public class ZipCodeWebPart : WebPart, IZipCode
  {
    string zipCodeText = String.Empty;
    TextBox input;
    Button send;

    public ZipCodeWebPart()
    {
    }

    // Make the implemented property personalizable to save 
    // the Zip Code between browser sessions.
    [Personalizable()]
    public virtual string ZipCode
    {
      get { return zipCodeText; }
      set { zipCodeText = value; }
    }

    // This is the callback method that returns the provider.
    [ConnectionProvider("Zip Code", "ZipCodeProvider")]
    public IZipCode ProvideIZipCode()
    {
      return this;
    }

    protected override void CreateChildControls()
    {
      Controls.Clear();
      input = new TextBox();
      this.Controls.Add(input);
      send = new Button();
      send.Text = "Enter 5-digit Zip Code";
      send.Click += new EventHandler(this.submit_Click);
      this.Controls.Add(send);
    }

    private void submit_Click(object sender, EventArgs e)
    {
      if (!string.IsNullOrEmpty(input.Text))
      {
        zipCodeText = Page.Server.HtmlEncode(input.Text);
        input.Text = String.Empty;
      }
    }
  }

  [AspNetHostingPermission(SecurityAction.Demand,
    Level = AspNetHostingPermissionLevel.Minimal)]
  [AspNetHostingPermission(SecurityAction.InheritanceDemand,
    Level = AspNetHostingPermissionLevel.Minimal)]
  public class WeatherWebPart : WebPart
  {
    private IZipCode _provider;
    string _zipSearch;
    Label DisplayContent;

    // This method is identified by the ConnectionConsumer 
    // attribute, and is the mechanism for connecting with 
    // the provider. 
    [ConnectionConsumer("Zip Code", "ZipCodeConsumer")]
    public void GetIZipCode(IZipCode Provider)
    {
      _provider = Provider;
    }
    
    protected override void OnPreRender(EventArgs e)
    {
      EnsureChildControls();

      if (this._provider != null)
      {
        _zipSearch = _provider.ZipCode.Trim();
        DisplayContent.Text = "My Zip Code is:  " + _zipSearch;
      }
    }

    protected override void CreateChildControls()
    {
      Controls.Clear();
      DisplayContent = new Label();
      this.Controls.Add(DisplayContent);
    }
  }
}
Imports System.Web
Imports System.Web.Security
Imports System.Security.Permissions
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts

Namespace Samples.AspNet.VB.Controls

  <AspNetHostingPermission(SecurityAction.Demand, _
    Level:=AspNetHostingPermissionLevel.Minimal)> _
  <AspNetHostingPermission(SecurityAction.InheritanceDemand, _
    Level:=AspNetHostingPermissionLevel.Minimal)> _
  Public Interface IZipCode

    Property ZipCode() As String

  End Interface

  <AspNetHostingPermission(SecurityAction.Demand, _
    Level:=AspNetHostingPermissionLevel.Minimal)> _
  <AspNetHostingPermission(SecurityAction.InheritanceDemand, _
    Level:=AspNetHostingPermissionLevel.Minimal)> _
  Public Class ZipCodeWebPart
    Inherits WebPart
    Implements IZipCode
    Private zipCodeText As String = String.Empty
    Private input As TextBox
    Private send As Button

    Public Sub New()
    End Sub

    ' Make the implemented property personalizable to save 
    ' the Zip Code between browser sessions.
    <Personalizable()> _
    Public Property ZipCode() As String _
      Implements IZipCode.ZipCode

      Get
        Return zipCodeText
      End Get
      Set(ByVal value As String)
        zipCodeText = value
      End Set
    End Property

    ' This is the callback method that returns the provider.
    <ConnectionProvider("Zip Code", "ZipCodeProvider")> _
    Public Function ProvideIZipCode() As IZipCode
      Return Me
    End Function


    Protected Overrides Sub CreateChildControls()
      Controls.Clear()
      input = New TextBox()
      Me.Controls.Add(input)
      send = New Button()
      send.Text = "Enter 5-digit Zip Code"
      AddHandler send.Click, AddressOf Me.submit_Click
      Me.Controls.Add(send)

    End Sub


    Private Sub submit_Click(ByVal sender As Object, _
      ByVal e As EventArgs)

      If input.Text <> String.Empty Then
        zipCodeText = Page.Server.HtmlEncode(input.Text)
        input.Text = String.Empty
      End If

    End Sub

  End Class

  <AspNetHostingPermission(SecurityAction.Demand, _
    Level:=AspNetHostingPermissionLevel.Minimal)> _
  <AspNetHostingPermission(SecurityAction.InheritanceDemand, _
    Level:=AspNetHostingPermissionLevel.Minimal)> _
  Public Class WeatherWebPart
    Inherits WebPart
    Private _provider As IZipCode
    Private _zipSearch As String
    Private DisplayContent As Label

    ' This method is identified by the ConnectionConsumer 
    ' attribute, and is the mechanism for connecting with 
    ' the provider. 
    <ConnectionConsumer("Zip Code", "ZipCodeConsumer")> _
    Public Sub GetIZipCode(ByVal Provider As IZipCode)
      _provider = Provider
    End Sub


    Protected Overrides Sub OnPreRender(ByVal e As EventArgs)
      EnsureChildControls()

      If Not (Me._provider Is Nothing) Then
        _zipSearch = _provider.ZipCode.Trim()
                DisplayContent.Text = "My Zip Code is:  " + _zipSearch
      End If

    End Sub

    Protected Overrides Sub CreateChildControls()
      Controls.Clear()
      DisplayContent = New Label()
      Me.Controls.Add(DisplayContent)

    End Sub

  End Class

End Namespace

The third part of the code example is the Web page. Notice that it contains two WebPartZone zones, with the first one containing the two custom WebPart controls. There is also a CatalogZone zone, which contains a standard Calendar control that users can add to the page. The <asp:connectionszone> element provides a connection UI for users to create connections between controls. In the Page_PreRender method, notice that it checks to see whether the personalization data has changed and, if so, updates the text of Label1.

<%@ Page Language="C#" %>
<%@ Register TagPrefix="uc1" 
    TagName="DisplayModeMenuCS"
    Src="~/displaymodemenucs.ascx" %>
<%@ Register TagPrefix="aspSample" 
    Namespace="Samples.AspNet.CS.Controls" %>
    
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

  protected void Page_PreRender(object sender, EventArgs e)
  {
    // Clear the label if it has a previously set value.
    Label1.Text = String.Empty;
    
    // Cast the WebPartManager to the IPersonalizable interface 
    // so that you can access the property.  
    IPersonalizable stateData = (IPersonalizable)mgr1;
    if (stateData.IsDirty)
      Label1.Text = "WebPartManager personalization data is dirty.";
  }
    
  protected void Button1_Click(object sender, EventArgs e)
  {
    ProviderConnectionPoint provPoint = 
      mgr1.GetProviderConnectionPoints(zip1)["ZipCodeProvider"];
    ConsumerConnectionPoint connPoint = 
      mgr1.GetConsumerConnectionPoints(weather1)["ZipCodeConsumer"];
    WebPartConnection conn1 = mgr1.ConnectWebParts(zip1, provPoint,
      weather1, connPoint);
  }
</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:WebPartManager ID="mgr1" runat="server" />
      <uc1:DisplayModeMenuCS ID="menu1" runat="server" />
      <asp:WebPartZone ID="WebPartZone1" runat="server">
        <ZoneTemplate>
          <aspSample:ZipCodeWebPart ID="zip1" runat="server"
            Title="Zip Code Provider" />
          <aspSample:WeatherWebPart ID="weather1" runat="server" 
            Title="Zip Code Consumer" />
        </ZoneTemplate>
      </asp:WebPartZone>
      <asp:WebPartZone ID="WebPartZone2" runat="server">
        <ZoneTemplate>
        </ZoneTemplate>
      </asp:WebPartZone>
      <asp:CatalogZone ID="CatalogZone1" runat="server">
        <ZoneTemplate>
          <asp:DeclarativeCatalogPart ID="DeclarativeCatalogPart1" 
            runat="server">
            <WebPartsTemplate>
              <asp:Calendar ID="Calendar1" runat="server" 
                Title="My Calendar" />
            </WebPartsTemplate>
          </asp:DeclarativeCatalogPart>
          <asp:PageCatalogPart ID="PageCatalogPart1" runat="server" />
        </ZoneTemplate>
      </asp:CatalogZone>
      <asp:ConnectionsZone ID="ConnectionsZone1" runat="server" />
      <asp:Button ID="Button1" runat="server" 
        Text="Connect WebPart Controls" 
        OnClick="Button1_Click" />
      <hr />
      <asp:Label ID="Label1" runat="server" 
        Text="" 
        Font-Bold="true" />
    </div>
    </form>
</body>
</html>
<%@ Page Language="vb" %>
<%@ Register TagPrefix="uc1" 
    TagName="DisplayModeMenuVB"
    Src="~/displaymodemenuvb.ascx" %>
<%@ Register TagPrefix="aspSample" 
    Namespace="Samples.AspNet.VB.Controls" %>
    
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

  Protected Sub Page_PreRender(ByVal sender As Object, _
    ByVal e As System.EventArgs)
    
    ' Clear the label if it has a previously set value.
    Label1.Text = String.Empty
    
    ' Cast the WebPartManager to the IPersonalizable interface 
    ' so that you can access the property.
    Dim stateData As IPersonalizable = CType(mgr1, IPersonalizable)
    If stateData.IsDirty Then
      Label1.Text = "WebPartManager personalization data is dirty."
    End If
    
  End Sub
    
  Protected Sub Button1_Click(ByVal sender As Object, _
    ByVal e As System.EventArgs)

    Dim provPoint As ProviderConnectionPoint = _
      mgr1.GetProviderConnectionPoints(zip1)("ZipCodeProvider")
    Dim connPoint As ConsumerConnectionPoint = _
      mgr1.GetConsumerConnectionPoints(weather1)("ZipCodeConsumer")
    Dim conn1 As WebPartConnection = _
      mgr1.ConnectWebParts(zip1, provPoint, weather1, connPoint)
      
  End Sub

</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:WebPartManager ID="mgr1" runat="server" />
      <uc1:DisplayModeMenuVB ID="menu1" runat="server" />
      <asp:WebPartZone ID="WebPartZone1" runat="server">
        <ZoneTemplate>
          <aspSample:ZipCodeWebPart ID="zip1" runat="server"
            Title="Zip Code Provider" />
          <aspSample:WeatherWebPart ID="weather1" runat="server" 
            Title="Zip Code Consumer" />
        </ZoneTemplate>
      </asp:WebPartZone>
      <asp:WebPartZone ID="WebPartZone2" runat="server">
        <ZoneTemplate>
        </ZoneTemplate>
      </asp:WebPartZone>
      <asp:CatalogZone ID="CatalogZone1" runat="server">
        <ZoneTemplate>
          <asp:DeclarativeCatalogPart ID="DeclarativeCatalogPart1" 
            runat="server">
            <WebPartsTemplate>
              <asp:Calendar ID="Calendar1" runat="server" 
                Title="My Calendar" />
            </WebPartsTemplate>
          </asp:DeclarativeCatalogPart>
          <asp:PageCatalogPart ID="PageCatalogPart1" runat="server" />
        </ZoneTemplate>
      </asp:CatalogZone>
      <asp:ConnectionsZone ID="ConnectionsZone1" runat="server" />
      <asp:Button ID="Button1" runat="server" 
        Text="Connect WebPart Controls" 
        OnClick="Button1_Click" />
      <hr />
      <asp:Label ID="Label1" runat="server" 
        Text="" 
        Font-Bold="true" />
    </div>
    </form>
</body>
</html>

After you load the page in a browser, try to create some of the scenarios listed in the Remarks section of this topic that will change the personalization data. As you make various changes, when a change involves one of the personalization scenarios tracked by the WebPartManager control, the text of the Label1 control is displayed to indicate that personalization data has changed. For example, you can:

  • Create a connection between controls by clicking the Connect WebPart Controls button.

  • Use the Display Mode drop-down list control to switch the page to catalog mode, and add the My Calendar control to the second WebPartZone zone.

  • Change the page back to browse mode, click on the verbs menu (shown with an arrow symbol in the title bar) for the My Calendar control, and select Close to close it and add it to the page catalog.

  • Return the page to catalog mode, and add the My Calendar control back to the page.

  • Use the Display Mode control to switch the page to design mode, and rearrange the layout of the controls by dragging one or more of them to another zone, or to a different position in the same zone.

Remarks

The IPersonalizable.IsDirty property provides a way for callers to determine whether personalization state data that is managed by the WebPartManager control has changed. When users personalize page-level details, for example by changing page layout, creating or deleting connections, and adding or deleting controls, the personalization data managed by the WebPartManager control changes. This is a pass-through method that returns to callers the value of the protected IsCustomPersonalizationStateDirty property, which cannot be directly accessed by callers.

Note

The IPersonalizable.IsDirty property does not indicate whether personalizable property values, or individual properties that affect the appearance of individual WebPart controls, have changed. Control-level personalization is tracked for each control individually. The IPersonalizable.IsDirty property indicates only whether personalization data that is at the page level and is managed by the WebPartManager control has changed.

The following list describes some common instances of personalization that would cause the IPersonalizable.IsDirty property to return a value of true, indicating that the WebPartManager control has some changed personalization data:

  • Closing a static WebPart control (or server or user control) on a page.

  • Restoring a closed static WebPart control from a page catalog back to a page.

  • Moving any control within its zone or to another zone.

  • Adding a control from a catalog of WebPart or server controls, or adding a control programmatically.

  • Creating a connection between two WebPart controls, either programmatically or by using the connection user interface (UI).

  • Deleting a connection between two WebPart controls, either programmatically or by using the connection UI.

To access this property value, you must cast the WebPartManager control instance to the IPersonalizable interface; you can then read the IsDirty property value.

Applies to

See also