語言

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 控制項並可重新加入頁面,而已刪除的控制實例永遠無法被重新加入頁面。

Note

依照 Web Parts 控制集實作,使用者刪除動態 WebPart 控制項的能力取決於使用者本人及該控制項被加入頁面的個人化範圍。 如果控制項是在頁面處於共享範圍內(由擁有權限的使用者)時新增的,那麼當頁面在使用者範圍內時,個別使用者無法刪除該控制項。

只有動態控制可以刪除。 動態控制項可透過程式化或使用者從目錄中新增控制項到頁面。 靜態控制是以標記或持久化格式宣告式加入頁面。 由於宣告式標籤永久存在於標記中,靜態控制項永遠無法刪除,但可以關閉並重新開啟。

適用於

另請參閱