Szerkesztés

Megosztás a következőn keresztül:


WebPartDisplayModeCollection Class

Definition

Contains a collection of WebPartDisplayMode objects. This class cannot be inherited.

public ref class WebPartDisplayModeCollection sealed : System::Collections::CollectionBase
public sealed class WebPartDisplayModeCollection : System.Collections.CollectionBase
type WebPartDisplayModeCollection = class
    inherit CollectionBase
Public NotInheritable Class WebPartDisplayModeCollection
Inherits CollectionBase
Inheritance
WebPartDisplayModeCollection

Examples

The following code example demonstrates working with the WebPartDisplayModeCollection class. The key point is that you must inherit from the WebPartManager class and override the CreateDisplayModes method to add a custom WebPartDisplayMode object to the WebPartDisplayModeCollection collection created by the WebPartManager control.

This 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; the other is a custom WebPartDisplayMode object to add to the page's default display modes.

  • An explanation of how the example works.

The source code for the first part of the code example, the user control that lets you change display modes, comes from another topic. For this code example to work, you need to obtain the .ascx file for the user control from the Walkthrough: Changing Display Modes on a Web Parts Page topic, and place the file in the same folder as the .aspx page in this code example.

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" 
    TagName="TextDisplay" 
    TagPrefix="uc2" %>
<%@ Register Src="DisplayModeMenuCS.ascx" 
    TagName="DisplayModeMenuCS" 
    TagPrefix="uc1" %>
<%@ Register Namespace="Samples.AspNet.CS.Controls" 
    TagPrefix="sample" 
    Assembly="CustomDisplayModeCS"  %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <uc1:DisplayModeMenuCS id="menu1" runat="server" />
    <div>
    <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">
            <ZoneTemplate>
              <uc2:TextDisplay ID="TextDisplay1" runat="server" />
            </ZoneTemplate>
          </asp:WebPartZone>
        </td>
        <td style="width: 100px; height: 123px">
          <asp:WebPartZone ID="WebPartZone2" runat="server" />
        </td>
      </tr>
    </table>
    <br />
    </div>
    </form>
</body>
</html>
<%@ Page Language="vb" %>
<%@ Register Src="TextDisplayvb.ascx" 
    TagName="TextDisplay" 
    TagPrefix="uc2" %>
<%@ Register Src="DisplayModeMenuVB.ascx" 
    TagName="DisplayModeMenuVB" 
    TagPrefix="uc1" %>
<%@ Register Namespace="Samples.AspNet.VB.Controls" 
    TagPrefix="sample" 
    Assembly="CustomDisplayModeVB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <uc1:DisplayModeMenuVB id="menu1" runat="server" />
    <div>
    <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">
            <ZoneTemplate>
              <uc2:TextDisplay ID="TextDisplay1" runat="server" />
            </ZoneTemplate>
          </asp:WebPartZone>
        </td>
        <td style="width: 100px; height: 123px">
          <asp:WebPartZone ID="WebPartZone2" runat="server" />
        </td>
      </tr>
    </table>
    <br />
    </div>
    </form>
</body>
</html>

The third part of the example is the user control for entering and displaying text. Notice that it uses a MultiView control to create multiple views of the user interface (UI). One view is displayed with the button, the other without. Notice that the in the overridden OnPreRender method, the code checks to see whether the page is currently in the custom display mode, and if so, it 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.

Important

This example has a text box that accepts user input, which is a potential security threat. By default, ASP.NET Web pages validate that user input does not include script or HTML elements. For more information, see Script Exploits Overview.

<%@ Control Language="C#" %>
<%@ Import Namespace="Samples.AspNet.CS.Controls" %>
    
<script runat="server">
  private string textContent;

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

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

    WebPartManager wpmg = 
      WebPartManager.GetCurrentWebPartManager(this.Page);
    NewWebPartManager myNewWpmg = wpmg as NewWebPartManager;
    if (myNewWpmg != null)
    {
      WebPartDisplayMode mode = 
        myNewWpmg.SupportedDisplayModes[myNewWpmg.InLineEditDisplayMode.Name];
      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 = 
      WebPartManager.GetCurrentWebPartManager(this.Page);
    WebPartDisplayMode mode = 
      wpmg.SupportedDisplayModes[WebPartManager.BrowseDisplayMode.Name];
    if (mode != null)
      wpmg.DisplayMode = mode;
  }
  
</script>
<asp:MultiView ID="MultiView1" runat="server">
    <asp:View ID="View1" runat="server">
        <asp:Label ID="Label1" runat="server" Text="Label" />
    </asp:View>
    <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" />
    </asp:View>
</asp:MultiView>
<%@ Control Language="vb" %>
<%@ Import Namespace="Samples.AspNet.VB.Controls" %>
    
<script runat="server">

  Private _textContent As String

  <Personalizable()> _
  Public Property TextContent() As String
    Get
      Return _textContent
    End Get
    Set(ByVal value As String)
      _textContent = Value
    End Set
  End Property
 
  Protected Overrides Sub OnPreRender(ByVal e As EventArgs)
    Label1.Text = Me.TextContent
    Dim viewIndex As Integer = 0
      
    Dim wpmg As WebPartManager = _
      WebPartManager.GetCurrentWebPartManager(Me.Page)
    Dim myNewWpmg As NewWebPartManager = _
      CType(wpmg, NewWebPartManager)
    If Not (myNewWpmg Is Nothing) Then
      Dim mode As WebPartDisplayMode = _
        myNewWpmg.SupportedDisplayModes(myNewWpmg.InLineEditDisplayMode.Name)
      If Not (mode Is Nothing) AndAlso _
        myNewWpmg.DisplayMode Is mode Then
        viewIndex = 1
      End If
    End If
    Me.MultiView1.ActiveViewIndex = viewIndex

  End Sub

  Protected Sub Button1_Click(ByVal sender As Object, _
    ByVal e As EventArgs)
    
    Me.TextContent = TextBox1.Text
    Dim wpmg As WebPartManager = _
      WebPartManager.GetCurrentWebPartManager(Me.Page)
    Dim mode As WebPartDisplayMode = _
      wpmg.SupportedDisplayModes(WebPartManager.BrowseDisplayMode.Name)
    If Not (mode Is Nothing) Then
      wpmg.DisplayMode = mode
    End If

  End Sub
  
</script>
<asp:MultiView ID="MultiView1" runat="server">
    <asp:View ID="View1" runat="server">
        <asp:Label ID="Label1" runat="server" Text="Label" />
    </asp:View>
    <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" />
    </asp:View>
</asp:MultiView>

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, 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
{
  [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

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 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 not in the custom display mode.

Remarks

The WebPartDisplayModeCollection class is designed to contain a collection of WebPartDisplayMode objects. It is used primarily by the WebPartManager control to manage collections of WebPartDisplayMode objects.

A display mode is a special view of a Web page that appears when it is assigned as the current display mode on the WebPartManager.DisplayMode property. Display modes are used in the Web Parts control set to create page views in which users can carry out special tasks, such as editing controls, or rearranging the layout of a page. The WebPartManager control defines several display modes, including BrowseDisplayMode, DesignDisplayMode, EditDisplayMode, CatalogDisplayMode, and ConnectDisplayMode. The collection of display modes is referenced by the WebPartManager.DisplayModes property.

On any particular Web page that uses Web Parts controls, only certain display modes are available. The default browse mode and the design mode are nearly always available, but the other display modes are available only if their corresponding zone types are present on the page. For more information, see the WebPartDisplayMode class overview.

The WebPartManager control keeps track of the available display modes on a page with its SupportedDisplayModes property. This property references a WebPartDisplayModeCollection object that contains all the supported display modes.

The WebPartDisplayModeCollection class does not have an exposed constructor, so you cannot create your own new instance of it. If you create a custom WebPartDisplayMode object and want it to be part of the collection of supported display modes in the WebPartManager control, you must inherit from the WebPartManager class, override the CreateDisplayModes method, call the base method to create the collection, and then add any custom display modes to the collection by using its Add method.

The WebPartDisplayModeCollection class has two public properties. The IsReadOnly property is a read-only property that indicates whether the collection is read-only. The overloaded Item[] property provides access to the members of the collection.

The WebPartDisplayModeCollection class also has several methods. The Add method, already mentioned, enables you to add WebPartDisplayMode objects to the collection. The Contains method determines whether a particular display mode exists in the collection. The CopyTo method copies the collection to an array of objects. The IndexOf method returns the index of a particular display mode within the collection. Finally, the Insert method enables you to insert a display mode object at a particular index in the collection.

Properties

Capacity

Gets or sets the number of elements that the CollectionBase can contain.

(Inherited from CollectionBase)
Count

Gets the number of elements contained in the CollectionBase instance. This property cannot be overridden.

(Inherited from CollectionBase)
InnerList

Gets an ArrayList containing the list of elements in the CollectionBase instance.

(Inherited from CollectionBase)
IsReadOnly

Gets a value indicating whether the collection is read-only.

Item[Int32]

Gets a specific member of the collection according to its index.

Item[String]

Gets a specific member of the collection according to a unique identifier.

List

Gets an IList containing the list of elements in the CollectionBase instance.

(Inherited from CollectionBase)

Methods

Add(WebPartDisplayMode)

Adds a WebPartDisplayMode object to the collection.

Clear()

Removes all objects from the CollectionBase instance. This method cannot be overridden.

(Inherited from CollectionBase)
Contains(WebPartDisplayMode)

Returns a value indicating whether a particular WebPartDisplayMode object exists in the collection.

CopyTo(WebPartDisplayMode[], Int32)

Copies the collection to an array of WebPartDisplayMode objects.

Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetEnumerator()

Returns an enumerator that iterates through the CollectionBase instance.

(Inherited from CollectionBase)
GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetType()

Gets the Type of the current instance.

(Inherited from Object)
IndexOf(WebPartDisplayMode)

Returns the position of a particular member of the collection.

Insert(Int32, WebPartDisplayMode)

Inserts a WebPartDisplayMode object into the collection at the specified index position.

MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
OnClear()

Performs additional custom processes when clearing the contents of the CollectionBase instance.

(Inherited from CollectionBase)
OnClearComplete()

Performs additional custom processes after clearing the contents of the CollectionBase instance.

(Inherited from CollectionBase)
OnInsert(Int32, Object)

Performs additional custom processes before inserting a new element into the CollectionBase instance.

(Inherited from CollectionBase)
OnInsertComplete(Int32, Object)

Performs additional custom processes after inserting a new element into the CollectionBase instance.

(Inherited from CollectionBase)
OnRemove(Int32, Object)

Performs additional custom processes when removing an element from the CollectionBase instance.

(Inherited from CollectionBase)
OnRemoveComplete(Int32, Object)

Performs additional custom processes after removing an element from the CollectionBase instance.

(Inherited from CollectionBase)
OnSet(Int32, Object, Object)

Performs additional custom processes before setting a value in the CollectionBase instance.

(Inherited from CollectionBase)
OnSetComplete(Int32, Object, Object)

Performs additional custom processes after setting a value in the CollectionBase instance.

(Inherited from CollectionBase)
OnValidate(Object)

Performs additional custom processes when validating a value.

(Inherited from CollectionBase)
RemoveAt(Int32)

Removes the element at the specified index of the CollectionBase instance. This method is not overridable.

(Inherited from CollectionBase)
ToString()

Returns a string that represents the current object.

(Inherited from Object)

Explicit Interface Implementations

ICollection.CopyTo(Array, Int32)

Copies the entire CollectionBase to a compatible one-dimensional Array, starting at the specified index of the target array.

(Inherited from CollectionBase)
ICollection.IsSynchronized

Gets a value indicating whether access to the CollectionBase is synchronized (thread safe).

(Inherited from CollectionBase)
ICollection.SyncRoot

Gets an object that can be used to synchronize access to the CollectionBase.

(Inherited from CollectionBase)
IList.Add(Object)

Adds an object to the end of the CollectionBase.

(Inherited from CollectionBase)
IList.Contains(Object)

Determines whether the CollectionBase contains a specific element.

(Inherited from CollectionBase)
IList.IndexOf(Object)

Searches for the specified Object and returns the zero-based index of the first occurrence within the entire CollectionBase.

(Inherited from CollectionBase)
IList.Insert(Int32, Object)

Inserts an element into the CollectionBase at the specified index.

(Inherited from CollectionBase)
IList.IsFixedSize

Gets a value indicating whether the CollectionBase has a fixed size.

(Inherited from CollectionBase)
IList.IsReadOnly

Gets a value indicating whether the CollectionBase is read-only.

(Inherited from CollectionBase)
IList.Item[Int32]

Gets or sets the element at the specified index.

(Inherited from CollectionBase)
IList.Remove(Object)

Removes the first occurrence of a specific object from the CollectionBase.

(Inherited from CollectionBase)

Extension Methods

Cast<TResult>(IEnumerable)

Casts the elements of an IEnumerable to the specified type.

OfType<TResult>(IEnumerable)

Filters the elements of an IEnumerable based on a specified type.

AsParallel(IEnumerable)

Enables parallelization of a query.

AsQueryable(IEnumerable)

Converts an IEnumerable to an IQueryable.

Applies to

See also