WebPartManager.IPersonalizable.IsDirty 属性

定义

获取一个值,该值指示 WebPartManager 控件管理的自定义个性化设置状态数据是否已在网页上更改。

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

属性值

一个布尔值,指示个性化设置状态数据是否已更改。

实现

示例

下面的代码示例演示 了 属性的 IPersonalizable.IsDirty 简单用法,以指示导致控件的个性化设置数据更改的一 WebPartManager 些常见页面个性化设置实例。

该代码示例包含四个部分:

  • 一个用户控件,允许您更改包含 Web 部件控件的页面上的显示模式。

  • 一个源文件,其中包含两个可以连接的自定义 WebPart 控件和一个接口的代码。

  • 承载所有控件的网页。

  • 有关代码示例工作原理的说明。

代码示例的第一部分是用于更改显示模式的用户控件。 可以从类概述的“示例”部分获取用户控件的 WebPartManager 源代码。 有关显示模式和用户控件工作原理的信息,请参阅 演练:更改 Web 部件页上的显示模式

该示例的第二部分是包含自定义控件和 接口的源文件。 请注意, IZipCode 接口公开了一个方法,在自定义 ZipCodeWebPart 控件中实现的此方法充当回调方法,以便在 ZipCodeWebPart 连接方案中充当提供程序。 另一个控件 WeatherWebPart充当连接中的使用者控件;它可以使用 提供 ZipCodeWebPart的特定接口。 在实际应用程序中, WeatherWebPart 可以使用提供商提供的个性化邮政编码值,然后向用户提供图形天气信息。

若要运行代码示例,必须编译此源代码。 可以显式编译它,并将生成的程序集放入网站的 Bin 文件夹或全局程序集缓存中。 或者,可以将源代码放在站点的“App_Code”文件夹中,并在运行时对其进行动态编译。 此代码示例使用动态编译;因此,请注意 Register ,网页顶部此组件的 指令仅 TagPrefix 包含 和 Namespace 属性,而不包含 属性 Assembly 。 有关演示如何编译的演练,请参阅 演练:开发和使用自定义 Web 服务器控件

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

代码示例的第三部分是网页。 请注意,它包含两 WebPartZone 个区域,第一个区域包含两个自定义 WebPart 控件。 还有一个 CatalogZone 区域,其中包含用户可以添加到页面的标准 Calendar 控件。 元素 <asp:connectionszone> 提供连接 UI,让用户在控件之间创建连接。 请注意,在 方法中 Page_PreRender ,它会检查个性化设置数据是否已更改,如果是,则更新 的文本 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>

在浏览器中加载页面后,请尝试创建本主题的“备注”部分中列出的一些方案,这些方案将更改个性化设置数据。 进行各种更改时,当更改涉及控件 WebPartManager 跟踪的个性化方案之一时,将显示控件的文本 Label1 以指示个性化设置数据已更改。 例如,你能够:

  • 单击“ 连接 Web 部件控件 ”按钮,在控件之间创建连接。

  • 使用 “显示模式 ”下拉列表控件将页面切换到目录模式,并将 “我的日历” 控件添加到第二 WebPartZone 个区域。

  • 将页面更改回浏览模式,单击“ 我的日历” 控件) 标题栏中显示带有箭头符号 (谓词菜单,然后选择“ 关闭 ”将其关闭并将其添加到页面目录。

  • 将页面返回到目录模式,并将 “我的日历” 控件添加回页面。

  • 使用 “显示模式” 控件将页面切换到设计模式,并通过将一个或多个控件拖动到另一个区域或同一区域中的不同位置来重新排列这些控件的布局。

注解

属性 IPersonalizable.IsDirty 为调用方提供了一种方法,用于确定控件 WebPartManager 管理的个性化设置状态数据是否已更改。 当用户个性化页面级详细信息(例如通过更改页面布局、创建或删除连接以及添加或删除控件)时,控件 WebPartManager 管理的个性化设置数据将发生更改。 这是一种传递方法,它向调用方返回受保护 IsCustomPersonalizationStateDirty 属性的值,调用方无法直接访问该值。

注意

属性 IPersonalizable.IsDirty 不指示可个性化属性值或影响单个控件外观的单个 WebPart 属性是否已更改。 将为每个控件单独跟踪控件级别的个性化设置。 属性 IPersonalizable.IsDirty 仅指示页面级别由 控件管理的 WebPartManager 个性化设置数据是否已更改。

以下列表描述了一些常见的个性化设置实例,这些实例将导致 IPersonalizable.IsDirty 属性返回 值 true,指示 WebPartManager 控件具有一些已更改的个性化设置数据:

  • 在页面上) 关闭静态 WebPart 控件 (或服务器或用户控件。

  • 将已关闭的静态 WebPart 控件从页面目录还原回页。

  • 将任何控件移动到其区域内或移到另一个区域。

  • 从 或 服务器控件的 WebPart 目录添加控件,或者以编程方式添加控件。

  • 在两 WebPart 个控件之间创建连接,无论是以编程方式,还是通过使用连接用户界面 (UI) 。

  • 以编程方式或使用连接 UI 删除两 WebPart 个控件之间的连接。

若要访问此属性值,必须将控件实例IPersonalizable强制转换为 WebPartManager 接口;然后可以读取IsDirty属性值。

适用于

另请参阅