WebPartManager.CreateDisplayModes Method ()


The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Creates the set of all possible display modes for a Web Parts application.

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

protected virtual WebPartDisplayModeCollection CreateDisplayModes()

Return Value

Type: System.Web.UI.WebControls.WebParts.WebPartDisplayModeCollection

A WebPartDisplayModeCollection that contains all the display modes that are supported.

This method creates the list of all possible display modes, not just the display modes supported on a particular page. For more information about the supported display modes, see the SupportedDisplayModes property.

By default, the Web Parts control set creates the following set of display modes to be used on Web Parts pages:

Developers can create custom display modes, either with or without accompanying custom zones that derive from the WebZone or ToolZone classes. To create a custom display mode, you must inherit from the WebPartDisplayMode class, and to add your display mode as a supported mode on a page, you must inherit from the WebPartManager class and override the CreateDisplayModes method.

When you add display modes by using the Add method, you should add them in the order that you want them to appear in any user interface (UI) controls (such as a ListBox control) that provide to users the possible display modes on a page.

The following code example demonstrates how to use the CreateDisplayModes method.

The code example has five parts:

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

  • A Web page that hosts the other controls.

  • A user control that resides in a WebPartZone zone on the Web page, and enables you to enter and display text in a label.

  • A source code file that contains two controls. One is a custom WebPartManager control, and the other is a custom WebPartDisplayMode object to add to the page's default display modes.

  • A source code file that contains two custom WebPart controls, and a custom interface.

  • An explanation of how the example works in a browser.

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 more 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 Web page. It contains two WebPartZone controls, both user controls, and the custom WebPartManager control. Notice the Register directives at the top of the page to reference the user controls and namespace for the compiled controls.

<%@ Page Language="C#" %>
<%@ Register Src="TextDisplaycs.ascx" 
    TagPrefix="uc2" %>
<%@ Register Src="DisplayModeMenuCS.ascx" 
    TagPrefix="uc1" %>
<%@ Register Namespace="Samples.AspNet.CS.Controls" 
    Assembly="CustomDisplayModeCS"  %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
    <form id="form1" runat="server">
    <uc1:DisplayModeMenuCS id="menu1" runat="server" />
    <sample:NewWebPartManager runat="server" ID="wpgm1" />
    <br />
    <table style="width: 100%">
      <tr valign="top" align="center" >
        <td style="width: 100px; height: 123px">
          <asp:WebPartZone ID="WebPartZone1" runat="server">
              <uc2:TextDisplay ID="TextDisplay1" runat="server" />
        <td style="width: 100px; height: 123px">
          <asp:WebPartZone ID="WebPartZone2" runat="server" />
    <br />

The third part of the example is the user control for entering and displaying text. It uses a MultiView control to create multiple views of the UI. One view appears with the Button1 button, the other without. Notice that in the overridden OnPreRender method, the code checks to see whether the page is currently in the custom display mode and, if so, displays the first view of the user control, which includes the button. If the page is not in the custom display mode, for example if the page is in browse or design mode, the button is hidden.

<%@ Control Language="C#" %>
<%@ Import Namespace="Samples.AspNet.CS.Controls" %>

<script runat="server">
  private string textContent;

  public string TextContent
    get { return textContent; }
    set { textContent = value; }

  protected override void OnPreRender(EventArgs e)
    Label1.Text = this.textContent;
    int viewIndex = 0;

    WebPartManager wpmg = 
    NewWebPartManager myNewWpmg = wpmg as NewWebPartManager;
    if (myNewWpmg != null)
      WebPartDisplayMode mode = 
      if (mode != null && myNewWpmg.DisplayMode == mode)
        viewIndex = 1;
    this.MultiView1.ActiveViewIndex = viewIndex;


  protected void Button1_Click(object sender, EventArgs e)
    this.TextContent = TextBox1.Text;

    WebPartManager wpmg = 
    WebPartDisplayMode mode = 
    if (mode != null)
      wpmg.DisplayMode = mode;

<asp:MultiView ID="MultiView1" runat="server">
    <asp:View ID="View1" runat="server">
        <asp:Label ID="Label1" runat="server" Text="Label" />
    <asp:View ID="View2" runat="server">
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" OnClick="Button1_Click" 
          runat="server" Text="Button" />

The fourth part of the example is the source file for the two custom classes. Notice that the custom WebPartManager class overrides the CreateDisplayModes method, and that it first calls the base method to add all the default display modes, and then adds the custom display mode. The custom display mode class, InLineEditDisplayMode, simply inherits from WebPartDisplayMode, sets the name of the display mode in the constructor, and overrides a number of the base properties to establish the characteristics of the custom display.

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. For a walkthrough that demonstrates how to compile, see Walkthrough: Developing and Using a Custom Web Server Control.

using System;
using System.Collections.Generic;
using System.Configuration;
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;

namespace Samples.AspNet.CS.Controls
  Level = AspNetHostingPermissionLevel.Minimal)]
    Level = AspNetHostingPermissionLevel.Minimal)]
  public class NewWebPartManager : WebPartManager 
    private static readonly WebPartDisplayMode _inLineEditDisplayMode =
      new InlineWebPartEditDisplayMode();

    public NewWebPartManager() {}

    protected override WebPartDisplayModeCollection CreateDisplayModes() 
      WebPartDisplayModeCollection displayModes = 
      return displayModes;

    public WebPartDisplayMode InLineEditDisplayMode
      get { return _inLineEditDisplayMode; }

    private sealed class InlineWebPartEditDisplayMode : WebPartDisplayMode
      public InlineWebPartEditDisplayMode()
        : base("Inline Edit Display")
      public override bool AllowPageDesign
        get { return true; }
      public override bool RequiresPersonalization
        get { return true; }
      public override bool ShowHiddenWebParts
        get { return false; }
      public override bool AssociatedWithToolZone
        get { return false; }
      public override bool IsEnabled(WebPartManager webPartManager)
        return true;



To run the code example, load the page in a browser. Notice that the page is currently in browse mode, and no button is visible. Using the Display Mode drop-down list control, change the page to Inline Edit Display mode, and notice that now the Button1 button is visible in the lower user control. Add some text, and click the button to update the control. Notice that the page display is returned to browse mode, the text you entered is now displayed, and the button is once again hidden because the page is no longer in the custom display mode.

.NET Framework
Available since 2.0
Return to top