WebPartManager.DeleteWebPart(WebPart) 方法

定义

永久地从网页中移除 WebPart 控件的动态实例。

public:
 void DeleteWebPart(System::Web::UI::WebControls::WebParts::WebPart ^ webPart);
public void DeleteWebPart (System.Web.UI.WebControls.WebParts.WebPart webPart);
member this.DeleteWebPart : System.Web.UI.WebControls.WebParts.WebPart -> unit
Public Sub DeleteWebPart (webPart As WebPart)

参数

webPart
WebPart

要删除的服务器控件。

示例

下面的代码示例演示如何使用 DeleteWebPart 方法。 第一次单击“ 添加日历” 按钮时,事件处理程序中的代码会创建一个 Calendar 控件,并将其作为 对象 GenericWebPart 添加到区域中。 由于控件是以编程方式添加的,因此它是动态控件,因此可以将其删除。 当用户单击“ 删除日历” 按钮时,代码将确保控件存在,然后通过调用 DeleteWebPart 方法将其删除。

<%@ 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 Button2_Click(object sender, EventArgs e)
  {
    WebPartManager mgr = WebPartManager1;
    Calendar cal = new Calendar();
    cal.ID = "cal1";
    GenericWebPart calWebPart = mgr.CreateWebPart(cal);
    mgr.AddWebPart(calWebPart, WebPartZone1, 1);
  }

  protected void Button1_Click(object sender, EventArgs e)
  {
    if (WebPartZone1.WebParts.Count > 1)
    {
      WebPart cal = WebPartZone1.WebParts[1];
      if (cal.Controls[0].GetType().Name == "Calendar" 
        && cal != null)
        WebPartManager1.DeleteWebPart(cal);
    }

  }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Adding a Server Control</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      <asp:WebPartManager ID="WebPartManager1" 
        runat="server" />
      <asp:WebPartZone ID="WebPartZone1" 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:Button ID="Button1" runat="server" 
        Text="Delete Calendar" 
        OnClick="Button1_Click" />
      <asp:Button ID="Button2" runat="server" 
        Text="Add Calendar" 
        OnClick="Button2_Click" />
    </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 Button2_Click(ByVal sender As Object, _
    ByVal e As System.EventArgs)
    Dim mgr As WebPartManager = WebPartManager1
    Dim cal As New Calendar()
    cal.ID = "cal1"
    Dim calWebPart As GenericWebPart = mgr.CreateWebPart(cal)
    mgr.AddWebPart(calWebPart, WebPartZone1, 1)
  End Sub

  Protected Sub Button1_Click(ByVal sender As Object, _
    ByVal e As System.EventArgs)

    If WebPartZone1.WebParts.Count > 1 Then
      Dim cal As WebPart = WebPartZone1.WebParts(1)
      If cal.Controls(0).GetType().Name = "Calendar" AndAlso _
        cal IsNot Nothing Then
        WebPartManager1.DeleteWebPart(cal)
      End If
    End If
  End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Adding a Server Control</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      <asp:WebPartManager ID="WebPartManager1" 
        runat="server" />
      <asp:WebPartZone ID="WebPartZone1" 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:Button ID="Button1" runat="server" 
        Text="Delete Calendar" 
        OnClick="Button1_Click" />
      <asp:Button ID="Button2" runat="server" 
        Text="Add Calendar" 
        OnClick="Button2_Click" />
    </div>
    </form>
</body>
</html>

注解

方法 DeleteWebPart 从页中永久删除 由 参数表示 webPart 的控件。 与已添加到控件 PageCatalogPart 并可以添加回页的已关闭控件不同,已删除的控件实例永远无法添加回页。

注意

正如由 Web 部件控件集实现的,用户删除动态 WebPart 控件的能力取决于用户以及控件添加到页面的个性化设置范围。 如果在) 权限的用户 (页面处于共享范围内时添加控件,则当页面处于用户范围内时,单个用户无法删除该控件。

只能删除动态控件。 动态控件以编程方式或由用户从目录中添加控件添加到页面。 静态控件以声明方式以标记或持久性格式添加到页面。 由于声明性标记永久存在于标记中,因此永远无法删除静态控件,但可以关闭并重新打开它们。

适用于

另请参阅