WebPart.AllowClose 属性

定义

获取或设置一个值,该值指示最终用户是否可以在网页上关闭 WebPart 控件。

public:
 virtual property bool AllowClose { bool get(); void set(bool value); };
[System.Web.UI.Themeable(false)]
[System.Web.UI.WebControls.WebParts.Personalizable(System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared)]
public virtual bool AllowClose { get; set; }
[<System.Web.UI.Themeable(false)>]
[<System.Web.UI.WebControls.WebParts.Personalizable(System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared)>]
member this.AllowClose : bool with get, set
Public Overridable Property AllowClose As Boolean

属性值

Boolean

如果可以在网页关闭该控件,则为 true;否则为 false。 默认值是 true

属性

示例

下面的代码示例演示如何更改自定义WebPart控件的属性默认设置AllowClose,以便无法关闭它。

The first part of this example contains the code for a custom WebPart control named TextDisplayWebPart. 请注意,在自定义控件的构造函数中,该 TextDisplayWebPart.AllowClose 属性设置为 false该属性,其效果是阻止用户关闭网页上的控件。 这意味着将为用户禁用控件的谓词菜单上的关闭谓词。 若要运行代码示例,必须编译此源代码。 可以显式编译它,并将生成的程序集放入网站的 Bin 文件夹或全局程序集缓存中。 或者,可以将源代码放在站点的App_Code文件夹中,该文件夹中将在运行时动态编译。 此代码示例假定将源代码编译为程序集,将其放置在 Web 应用程序的 Bin 子文件夹中,并在网页中使用指令引用程序集 Register 。 有关演示两种编译方法的演练,请参阅 演练:开发和使用自定义 Web 服务器控件

using System;
using System.Security.Permissions;
using System.Web;
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 TextDisplayWebPart : WebPart
  {
    private String _contentText = null;
    TextBox input;
    Label DisplayContent;

    public TextDisplayWebPart()
    {
      this.AllowClose = false;
    }

    [Personalizable(), WebBrowsable]
    public String ContentText
    {
      get { return _contentText; }
      set { _contentText = value; }
    }

    protected override void CreateChildControls()
    {
      Controls.Clear();
      DisplayContent = new Label();
      DisplayContent.BackColor = 
        System.Drawing.Color.LightBlue;
      DisplayContent.Text = this.ContentText;
      this.Controls.Add(DisplayContent);
      input = new TextBox();
      this.Controls.Add(input);
      Button update = new Button();
      update.Text = "Set Label Content";
      update.Click += new EventHandler(this.submit_Click);
      this.Controls.Add(update);
      ChildControlsCreated = true;
    }

    private void submit_Click(object sender, EventArgs e)
    {
      // Update the label string.
      if (!string.IsNullOrEmpty(input.Text))
      {
        _contentText = input.Text + @"<br />";
        input.Text = String.Empty;
        DisplayContent.Text = this.ContentText;
      }
    }
  }
}
Imports System.Security.Permissions
Imports System.Web
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 TextDisplayWebPart
    Inherits WebPart
    Private _contentText As String = Nothing
    Private input As TextBox
    Private DisplayContent As Label
    
    
    Public Sub New() 
      Me.AllowClose = False
    End Sub
    
    <Personalizable(), WebBrowsable()>  _
    Public Property ContentText() As String 
        Get
            Return _contentText
        End Get
        Set
            _contentText = value
        End Set
    End Property
     
    Protected Overrides Sub CreateChildControls() 
        Controls.Clear()
        DisplayContent = New Label()
        DisplayContent.Text = Me.ContentText
        DisplayContent.BackColor = _
          System.Drawing.Color.LightBlue
        Me.Controls.Add(DisplayContent)
        input = New TextBox()
        Me.Controls.Add(input)
        Dim update As New Button()
        update.Text = "Set Label Content"
        AddHandler update.Click, AddressOf Me.submit_Click
        Me.Controls.Add(update)
        ChildControlsCreated = True
    
    End Sub
    
    
    Private Sub submit_Click(ByVal sender As Object, _
                             ByVal e As EventArgs) 
        ' Update the label string.
        If input.Text <> String.Empty Then
            _contentText = input.Text & "<br />"
            input.Text = String.Empty
            DisplayContent.Text = Me.ContentText
        End If
    
    End Sub
    
End Class

End Namespace

本示例的第二部分演示如何引用 TextDisplayWebPart ASP.NET 网页中的控件。 请注意,在引用控件的元素中 <aspSample:TextDisplayWebPart> ,还可以更改控件构造函数设置的属性的值。 若要允许关闭控件,只需将属性 AllowClose="true" 添加到声明性标记中的元素即可。

<%@ page language="C#" %>
<%@ register tagprefix="aspSample" 
             Namespace="Samples.AspNet.CS.Controls" 
             Assembly="TextDisplayWebPartCS"%>

<!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 id="Head1" runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
  <form id="Form1" runat="server">
    <asp:webpartmanager id="WebPartManager1" runat="server" />
    <asp:webpartzone
      id="WebPartZone1"
      runat="server"
      title="Zone 1"
      PartChromeType="TitleAndBorder">
        <parttitlestyle font-bold="true" ForeColor="#3300cc" />
        <partstyle
          borderwidth="1px"   
          borderstyle="Solid"  
          bordercolor="#81AAF2" />
        <zonetemplate>
          <aspSample:TextDisplayWebPart 
            runat="server"   
            id="textwebpart" 
            title = "Text Content WebPart" />
        </zonetemplate>
    </asp:webpartzone>
  </form>
</body>
</html>
<%@ page language="VB" %>
<%@ register tagprefix="aspSample" 
             Namespace="Samples.AspNet.VB.Controls" 
             Assembly="TextDisplayWebPartVB"%>

<!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 id="Head1" runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
  <form id="Form1" runat="server">
    <asp:webpartmanager id="WebPartManager1" runat="server" />
    <asp:webpartzone
      id="WebPartZone1"
      runat="server"
      title="Zone 1"
      PartChromeType="TitleAndBorder">
        <parttitlestyle font-bold="true" ForeColor="#3300cc" />
        <partstyle
          borderwidth="1px"   
          borderstyle="Solid"  
          bordercolor="#81AAF2" />
        <zonetemplate>
          <aspSample:TextDisplayWebPart 
            runat="server"   
            id="textwebpart" 
            title = "Text Content WebPart" />
        </zonetemplate>
    </asp:webpartzone>
  </form>
</body>
</html>

注解

用户关闭网页上的 WebPart 控件后,该控件将不再可见或显示在页面上。 关闭的控件将添加到页面目录,这是存储对控件引用的Web 部件实体。 如果开发人员将 PageCatalogPart 控件添加到控件中的 CatalogZone 页面,则用户能够将页面切换到目录显示模式,在页面目录中选择关闭的控件,并将其添加回页面。

备注

可以编程方式将关闭 WebPart 的控件添加回页面,也可以由在页面目录处于目录显示模式时从页面目录中选择关闭控件的用户添加。

WebPart关闭控件与删除控件不同。 关闭的控件可以添加回页面,同时永久删除已删除的控件。 有关删除控件的详细信息,请参阅 DeleteWebPart 该方法。 关闭控件也不同于隐藏控件。 隐藏的控件仍存在于页面上,仍参与页面生命周期事件,并且仅隐藏在用户的视图中,但关闭的控件甚至不会呈现在页面上。

静态控件和动态 WebPart 控件 (静态控件在页面标记中声明,而动态控件以编程方式添加) 可以关闭。

如果开发人员将 AllowClose 属性设置为该属性 false,则控件上不会显示关闭谓词,并且用户无法关闭控件。

无法通过主题或样式表主题设置此属性。 有关详细信息,请参阅ThemeableAttributeASP.NET 主题和外观

此属性的个性化设置范围设置为 Shared 授权用户,并且只能由授权用户修改。 有关详细信息,请参阅PersonalizableAttributeWeb 部件个性化设置概述

适用于

另请参阅