WebPartDisplayModeCollection.Add(WebPartDisplayMode) Method

Definition

Adds a WebPartDisplayMode object to the collection.

public:
 int Add(System::Web::UI::WebControls::WebParts::WebPartDisplayMode ^ value);
public int Add (System.Web.UI.WebControls.WebParts.WebPartDisplayMode value);
member this.Add : System.Web.UI.WebControls.WebParts.WebPartDisplayMode -> int
Public Function Add (value As WebPartDisplayMode) As Integer

Parameters

value
WebPartDisplayMode

A WebPartDisplayMode to add to the collection.

Returns

An integer value that indicates where the WebPartDisplayMode was inserted into the collection.

Examples

This code example demonstrates the use of the WebPartDisplayModeCollection class. For the full code and instructions required to run the example, see the Example section of the WebPartDisplayModeCollection class overview.

The following section of code occurs in a derived WebPartManager class that overrides the CreateDisplayModes method and adds a custom display mode to the collection.

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
{
  [AspNetHostingPermission(SecurityAction.Demand,
  Level = AspNetHostingPermissionLevel.Minimal)]
  [AspNetHostingPermission(SecurityAction.InheritanceDemand,
    Level = AspNetHostingPermissionLevel.Minimal)]
  public class NewWebPartManager : WebPartManager 
  {
    private static readonly WebPartDisplayMode _inLineEditDisplayMode =
      new InlineWebPartEditDisplayMode();

    public NewWebPartManager() {}

    protected override WebPartDisplayModeCollection CreateDisplayModes() 
    {
      WebPartDisplayModeCollection displayModes = 
        base.CreateDisplayModes();
      displayModes.Add(_inLineEditDisplayMode);
      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;
      }
    }
  }
}
Imports System.Collections.Generic
Imports System.Configuration
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 Class NewWebPartManager
    Inherits WebPartManager
    Private Shared _inLineEditDisplayMode As WebPartDisplayMode = _
      New InlineWebPartEditDisplayMode()

    Public Sub New()
    End Sub

    Protected Overrides Function CreateDisplayModes() As WebPartDisplayModeCollection
      Dim displayModes As WebPartDisplayModeCollection = MyBase.CreateDisplayModes()
      displayModes.Add(_inLineEditDisplayMode)
      Return displayModes

    End Function 

    Public ReadOnly Property InLineEditDisplayMode() As WebPartDisplayMode
        Get
            Return _inLineEditDisplayMode
        End Get
    End Property

    Private NotInheritable Class InlineWebPartEditDisplayMode
      Inherits WebPartDisplayMode

      Public Sub New()
        MyBase.New("Inline Edit Display")
      End Sub

      Public Overrides ReadOnly Property AllowPageDesign() As Boolean
        Get
          Return True
        End Get
      End Property

      Public Overrides ReadOnly Property RequiresPersonalization() _
        As Boolean
        Get
          Return True
        End Get
      End Property

      Public Overrides ReadOnly Property ShowHiddenWebParts() As Boolean
        Get
          Return False
        End Get
      End Property

      Public Overrides ReadOnly Property AssociatedWithToolZone() _
        As Boolean
        Get
          Return False
        End Get
      End Property

      Public Overrides Function IsEnabled(ByVal webPartManager _
        As WebPartManager) As Boolean

        Return True

      End Function

    End Class

  End Class

End Namespace

Remarks

You can use the Add method to add new WebPartDisplayMode objects to an existing WebPartDisplayModeCollection object.

An example of a scenario in which you would do this is when you create a custom WebPartDisplayMode class, and you want to add it to the collection of supported display modes on a page. To do that, you must inherit from the WebPartManager class and override its CreateDisplayModes method, first calling the base method, and then using the Add method to add the custom display mode to the collection.

Applies to

See also