SharePoint Tip #15: Testing the Display Mode of a Web Part

When developing a web part (whether in WSS 3.0 or in ASP.Net 2.0) it can be important that it appears differently if the user is in edit mode. The simplest way to do this is to test the WebPartManager's DisplayMode in the web part's Render or CreateChildControls method.

protected override void CreateChildControls()
{
  ...
  if (this.WebPartManager.DisplayMode ==
      WebPartManager.BrowseDisplayMode)
  {
    // normal view mode (the page is not in edit mode)
  }
  else if (this.WebPartManager.DisplayMode ==
      WebPartManager.DesignDisplayMode)
  {
    // the page is in edit mode.
    // web parts can be moved between WebPartZones
  }
  else if (this.WebPartManager.DisplayMode ==
      WebPartManager.EditDisplayMode)
  {
    // the page and the web parts contained in it are in edit mode,
    // allowing access to specific actions of each one

  }
}

In the above example I'm testing the WebPartManager's DisplayMode in the web part's CreateChildControls method. When developing for WSS, it is most common to place the configuration controls only in the DesignDisplayMode, which means that the Edit Page button was pressed.

Leave a Reply

Your email address will not be published. Required fields are marked *