IPersonalizable.IsDirty Property

Gets a value that indicates whether the custom data that a control manages has changed.

Namespace: System.Web.UI.WebControls.WebParts
Assembly: System.Web (in system.web.dll)

bool IsDirty { get; }
/** @property */
boolean get_IsDirty ()

function get IsDirty () : boolean

Not applicable.

Property Value

true if the custom data managed with the IPersonalizable interface has changed; otherwise, false.

When data managed by a control has changed, the control is considered "dirty." A control should return true if the custom data that it manages through the IPersonalizable interface has changed. If a control returns true from this property, the control's Save interface method implementation is called during property value extraction.


This property is checked automatically by the personalization infrastructure during the latter phases of processing a POST request. However, for GET requests, the value returned from the IsDirty property is not checked. Some other event has to have caused the control to be considered "dirty" for any control state--not just data managed by IPersonalizable--to be saved. If a control can mark itself as "dirty" during a GET request, or if the personalization infrastructure under some very specific boundary conditions considers a control "dirty" during a GET request, then Save will always be called.

A control can reference its associated WebPartManager control and check the Scope property to determine the current scope. The "dirty" state of a control's custom data should be appropriate for the current scope.

The following code example demonstrates use of the IsDirty property. The first part of the example is the Web page that hosts a WebPart control.

<%@ Page Language="C#"  %>
<%@ Register TagPrefix="dict" 
    namespace="Samples.AspNet.CS.Controls" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <form id="form1" runat="server">
    <asp:WebPartManager ID="mgr" runat="server" />
    <asp:WebPartZone ID="WebPartZone1" runat="server">
        <dict:urllistwebpart id="listwp1" runat="server"
          title="URL List WebPart" />

The second part of the example is the code for the custom WebPart control called UrlListWebPart. This file should be placed in the App_Code directory to run the example. Note that the source code implements the IsDirty property.

namespace Samples.AspNet.CS.Controls

    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Diagnostics;
    using System.Drawing;
    using System.Web;
    using System.Web.UI;
    using System.Security.Permissions;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;

      Level = AspNetHostingPermissionLevel.Minimal)]
      Level = AspNetHostingPermissionLevel.Minimal)]
    public class UrlListWebPart : WebPart, IPersonalizable
        private ArrayList _sharedUrls;
        private ArrayList _userUrls;
        private bool _listDirty;

        private TextBox _nameTextBox;
        private TextBox _urlTextBox;
        private Button _addButton;
        private BulletedList _list;

        protected override void CreateChildControls()
            Label nameLabel = new Label();
            Label urlLabel = new Label();
            LiteralControl breakLiteral1 = new LiteralControl("<br />");
            LiteralControl breakLiteral2 = new LiteralControl("<br />");
            LiteralControl breakLiteral3 = new LiteralControl("<br />");

            _nameTextBox = new TextBox();
            _urlTextBox = new TextBox();
            _addButton = new Button();
            _list = new BulletedList();

            nameLabel.Text = "Name: ";
            urlLabel.Text = "URL: ";
            _nameTextBox.ID = "nameTextBox";
            _urlTextBox.ID = "urlTextBox";
            _addButton.Text = "Add";
            _addButton.ID = "addButton";
            _addButton.Click += new EventHandler(this.OnClickAddButton);
            _list.DisplayMode = BulletedListDisplayMode.HyperLink;
            _list.ID = "list";





        private void OnClickAddButton(object sender, EventArgs e)
            string name = _nameTextBox.Text.Trim();
            string url = _urlTextBox.Text.Trim();

            Pair p = new Pair(name, url);
            if (WebPartManager.Personalization.Scope == PersonalizationScope.Shared)
                if (_sharedUrls == null)
                    _sharedUrls = new ArrayList();
                if (_userUrls == null)
                    _userUrls = new ArrayList();


        protected virtual void OnUrlAdded()
            _listDirty = true;
            ChildControlsCreated = false;

        protected override void RenderContents(HtmlTextWriter writer)
            if (_sharedUrls != null)
                foreach (Pair p in _sharedUrls)
                    _list.Items.Add(new ListItem((string)p.First, (string)p.Second));
            if (_userUrls != null)
                foreach (Pair p in _userUrls)
                    _list.Items.Add(new ListItem((string)p.First, (string)p.Second));


        public virtual bool IsDirty
                return _listDirty;
        public new virtual void Load(PersonalizationDictionary state)
            if (state != null)
                PersonalizationEntry sharedUrlsEntry = state["sharedUrls"];
                if (sharedUrlsEntry != null)
                    _sharedUrls = (ArrayList)sharedUrlsEntry.Value;

                PersonalizationEntry userUrlsEntry = state["userUrls"];
                if (userUrlsEntry != null)
                    _userUrls = (ArrayList)userUrlsEntry.Value;

        public virtual void Save(PersonalizationDictionary state)
            if ((_sharedUrls != null) && (_sharedUrls.Count != 0))
                state["sharedUrls"] = new PersonalizationEntry(_sharedUrls, PersonalizationScope.Shared);
            if ((_userUrls != null) && (_userUrls.Count != 0))
                state["userUrls"] = new PersonalizationEntry(_userUrls, PersonalizationScope.User);

Load the page in a browser. Type in a name to represent a URL, then add an actual URL beginning with http://, and click the Add button to add the URL.

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0