WebPartManager.EditDisplayMode 字段

定义

表示最终用户可在其中编辑和修改服务器控件的显示模式。 此字段为只读。

public: static initonly System::Web::UI::WebControls::WebParts::WebPartDisplayMode ^ EditDisplayMode;
public static readonly System.Web.UI.WebControls.WebParts.WebPartDisplayMode EditDisplayMode;
 staticval mutable EditDisplayMode : System.Web.UI.WebControls.WebParts.WebPartDisplayMode
Public Shared ReadOnly EditDisplayMode As WebPartDisplayMode 

字段值

示例

下面的代码示例演示如何以编程方式处理 EditDisplayMode 字段。 该代码使用页面支持的显示模式(在本例中为浏览、设计和编辑)填充下拉列表。 为了支持编辑,页面中有一个 <asp:EditorZone> 元素。 请注意,在 方法中 Page_PreRender ,代码检查当前 DisplayMode 属性是否设置为 EditDisplayMode。 如果是这样, Label1 将可见,否则 Label1 将隐藏。

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

  protected void Page_Init(object sender, EventArgs e)
  {
    foreach (WebPartDisplayMode mode in mgr.SupportedDisplayModes)
    {
      string modeName = mode.Name;
      if (mode.IsEnabled(mgr))
      {
        ListItem item = new ListItem(modeName, modeName);
        DisplayModeDropdown.Items.Add(item);
      }      
    }
  }

  protected void DisplayModeDropdown_SelectedIndexChanged(object 
    sender, EventArgs e)
  {
    String selectedMode = DisplayModeDropdown.SelectedValue;
    WebPartDisplayMode mode = 
      mgr.SupportedDisplayModes[selectedMode];
    if (mode != null)
      mgr.DisplayMode = mode;
  }

  protected void Page_PreRender(object sender, EventArgs e)
  {
    if (mgr.DisplayMode == WebPartManager.EditDisplayMode)
      Label1.Visible = true;
    else
      Label1.Visible = false;
  }
  
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      <asp:WebPartManager ID="mgr" runat="server">
      </asp:WebPartManager>
      <asp:WebPartZone ID="WebPartZone1" runat="server">
        <ZoneTemplate>
          <asp:Calendar ID="Calendar1" runat="server" 
            Title="My Calendar" />
        </ZoneTemplate>
      </asp:WebPartZone>
      <asp:WebPartZone ID="WebPartZone2" runat="server">
        <ZoneTemplate>
          <asp:BulletedList 
            DisplayMode="HyperLink" 
            ID="BulletedList1" 
            runat="server"
            Title="My Links">
            <asp:ListItem Value="http://www.microsoft.com">
            Microsoft
            </asp:ListItem>
            <asp:ListItem Value="http://www.msn.com">
            MSN
            </asp:ListItem>
            <asp:ListItem Value="http://www.contoso.com">
            Contoso Corp.
            </asp:ListItem>
          </asp:BulletedList>
        </ZoneTemplate>
      </asp:WebPartZone>
      <asp:EditorZone ID="EditorZone1" runat="server">
        <ZoneTemplate>
          <asp:AppearanceEditorPart runat="server" 
            ID="Appearance1">
          </asp:AppearanceEditorPart>
          <asp:LayoutEditorPart runat="server" ID="Layout1">
          </asp:LayoutEditorPart>
        </ZoneTemplate>
      </asp:EditorZone>
      <hr />
      <asp:Label ID="Label1" runat="server" 
        Text="Currently in Edit Mode" 
        Font-Bold="true"
        Font-Size="125%" />
      <br />
      <asp:DropDownList ID="DisplayModeDropdown" runat="server" 
        AutoPostBack="true"
        Width="120"
        OnSelectedIndexChanged=
        "DisplayModeDropdown_SelectedIndexChanged">
      </asp:DropDownList>
    </div>
    </form>
</body>
</html>
<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

  Protected Sub Page_Init(ByVal sender As Object, _
    ByVal e As EventArgs)
    Dim mode As WebPartDisplayMode
    For Each mode In mgr.SupportedDisplayModes
      Dim modeName As String = mode.Name
      If mode.IsEnabled(mgr) Then
        Dim item As ListItem = New ListItem(modeName, modeName)
        DisplayModeDropdown.Items.Add(item)
      End If
    Next
    
  End Sub

  Protected Sub DisplayModeDropdown_SelectedIndexChanged(ByVal _
    sender As Object, ByVal e As EventArgs)
    Dim selectedMode As String = _
      DisplayModeDropdown.SelectedValue
    Dim mode As WebPartDisplayMode = _
      mgr.SupportedDisplayModes(selectedMode)
    If mode IsNot Nothing Then
      mgr.DisplayMode = mode
    End If
  End Sub
  
  Protected Sub Page_PreRender(ByVal sender As Object, _
    ByVal e As System.EventArgs)
    If mgr.DisplayMode.Equals(WebPartManager.EditDisplayMode) Then
      Label1.Visible = True
    Else
      Label1.Visible = False
    End If
  End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      <asp:WebPartManager ID="mgr" runat="server">
      </asp:WebPartManager>
      <asp:WebPartZone ID="WebPartZone1" runat="server">
        <ZoneTemplate>
          <asp:Calendar ID="Calendar1" runat="server" 
            Title="My Calendar" />
        </ZoneTemplate>
      </asp:WebPartZone>
      <asp:WebPartZone ID="WebPartZone2" runat="server">
        <ZoneTemplate>
          <asp:BulletedList 
            DisplayMode="HyperLink" 
            ID="BulletedList1" 
            runat="server"
            Title="My Links">
            <asp:ListItem Value="http://www.microsoft.com">
            Microsoft
            </asp:ListItem>
            <asp:ListItem Value="http://www.msn.com">
            MSN
            </asp:ListItem>
            <asp:ListItem Value="http://www.contoso.com">
            Contoso Corp.
            </asp:ListItem>
          </asp:BulletedList>
        </ZoneTemplate>
      </asp:WebPartZone>
      <asp:EditorZone ID="EditorZone1" runat="server">
        <ZoneTemplate>
          <asp:AppearanceEditorPart runat="server" 
            ID="Appearance1">
          </asp:AppearanceEditorPart>
          <asp:LayoutEditorPart runat="server" ID="Layout1">
          </asp:LayoutEditorPart>
        </ZoneTemplate>
      </asp:EditorZone>
      <hr />
      <asp:Label ID="Label1" runat="server" 
        Text="Currently in Edit Mode" 
        Font-Bold="true"
        Font-Size="125%" />
      <br />
      <asp:DropDownList ID="DisplayModeDropdown" runat="server" 
        AutoPostBack="true"
        Width="120"
        OnSelectedIndexChanged=
        "DisplayModeDropdown_SelectedIndexChanged">
      </asp:DropDownList>
    </div>
    </form>
</body>
</html>

在浏览器中加载页面后,默认情况下处于浏览模式。 请注意,页面上的标签已隐藏。 使用下拉列表控件将页面切换到编辑模式。 请注意,由于 方法中的 Page_PreRender 代码,标签现在可见。 单击其中一个控件上谓词菜单中的 “编辑 谓词”,以启用该特定控件的编辑。

注解

字段EditDisplayMode引用控件创建并包含的WebPartManager自定义WebPartDisplayMode对象。 由于这是静态对象,因此可以直接通过 WebPartManager 类引用它,而无需控件的实例。

当包含 Web 部件控件的页面首次加载时,它默认处于 BrowseDisplayMode (浏览模式) 。 当用户想要编辑或修改服务器控件时,他们必须先将页面切换到 EditDisplayMode (编辑模式) 。 其次,他们必须通过单击该控件标头中的谓词菜单上的编辑谓词来选择要编辑的特定服务器控件。 控件处于编辑模式后,将显示用于编辑所选控件的编辑用户界面 (UI) 。

若要在页面上启用编辑模式,页面必须至少包含一个 EditorZone 区域,其中包含一个或多个提供的编辑控件,例如 LayoutEditorPart 控件或自定义编辑控件。

适用于

另请参阅