WebPartManager.AddWebPart(WebPart, WebPartZoneBase, Int32) Metodo

Definizione

Fornisce il metodo standard a livello di codice per aggiungere controlli WebPart a una pagina Web.Provides the standard programmatic method for adding WebPart controls to a Web page.

public:
 System::Web::UI::WebControls::WebParts::WebPart ^ AddWebPart(System::Web::UI::WebControls::WebParts::WebPart ^ webPart, System::Web::UI::WebControls::WebParts::WebPartZoneBase ^ zone, int zoneIndex);
public System.Web.UI.WebControls.WebParts.WebPart AddWebPart (System.Web.UI.WebControls.WebParts.WebPart webPart, System.Web.UI.WebControls.WebParts.WebPartZoneBase zone, int zoneIndex);
member this.AddWebPart : System.Web.UI.WebControls.WebParts.WebPart * System.Web.UI.WebControls.WebParts.WebPartZoneBase * int -> System.Web.UI.WebControls.WebParts.WebPart

Parametri

webPart
WebPart

Oggetto WebPart (oppure controllo server o controllo utente) da aggiungere a una pagina Web o da aprire in una pagina.The WebPart (or server or user control) to be added to a Web page or opened on a page.

zone
WebPartZoneBase

Classe WebPartZoneBase a cui viene aggiunto webPart.The WebPartZoneBase that webPart is being added to.

zoneIndex
Int32

Integer che rappresenta la posizione ordinale occupata dall'oggetto webPart in zone, relativa ad altri controlli presenti in zone.An integer that represents the ordinal position that webPart occupies in zone, relative to other controls in zone.

Restituisce

Controllo WebPart aggiunto alla pagina.A WebPart control that was added to the page.

Eccezioni

webPart è null.webPart is null.

-oppure--or- zone è null.zone is null.

zone non è registrato nell'insieme delle aree del controllo WebPartManager.zone is not registered in the WebPartManager control's collection of zones.

-oppure--or- webPart è già in zone.webPart is already in zone.

Il valore dizoneIndex è minore di zero.The value of zoneIndex is less than zero.

Esempi

Nell'esempio di codice riportato di seguito viene AddWebPart illustrato l'utilizzo del metodo per aggiungere un controllo server a livello di programmazione a una pagina.The following code example demonstrates use of the AddWebPart method to add a server control programmatically to a page. Il markup della pagina contiene un <asp:webpartzone> elemento vuoto e un <asp:webpartmanager> elemento.The page markup contains an empty <asp:webpartzone> element, and an <asp:webpartmanager> element. La prima volta che si fa clic sul pulsante Aggiungi calendario , il codice nel gestore eventi crea un Calendar controllo e lo aggiunge a GenericWebPart una zona come oggetto, chiamando il AddWebPart metodo.The first time the Add Calendar button is clicked, the code in the event handler creates a Calendar control, and adds it to a zone as a GenericWebPart object, calling the AddWebPart method.

<%@ 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>

Commenti

Il AddWebPart metodo viene usato sia per aggiungere nuovi controlli WebPart dinamici a una pagina che per riaprire i controlli statici o dinamici che in precedenza sono stati chiusi in una pagina.The AddWebPart method is used both to add new dynamic WebPart controls to a page, and to reopen static or dynamic controls that have previously been closed on a page. Quando viene chiamato il metodo per aggiungere un nuovo controllo, viene effettivamente creata una copia del controllo a cui si fa riferimento webPart nel parametro.When the method is called to add a new control, it actually creates a copy of the control referenced in the webPart parameter. Viene generato un nuovo ID per la copia del controllo, in modo che gli sviluppatori debbano fare riferimento al WebPart controllo restituito dal metodo per ottenere il nuovo valore ID.A new ID is generated for the copy of the control, so developers should reference the WebPart control returned from the method to get the new ID value. Quando viene chiamato il metodo per riaprire un controllo precedentemente chiuso, viene restituito un riferimento diretto al controllo a cui fa riferimento il webPart parametro.When the method is called to reopen a previously closed control, it returns a direct reference to the control referenced by the webPart parameter.

Importante

È consigliabile usare sempre il AddWebPart metodo, anziché il Add metodo della raccolta di controlli a cui fa riferimento la WebPartManager.Controls proprietà, per aggiungere WebPart controlli a livello di codice alla pagina, perché l' Add uso di il metodo genera un'eccezione.You should always use the AddWebPart method, rather than the Add method of the collection of controls referenced by the WebPartManager.Controls property, to add WebPart controls programmatically to the page, because using the Add method throws an exception. Per aggiungere un controllo che non è un WebPart controllo (in altre parole, un controllo server che verrà incluso in un GenericWebPart controllo in fase di esecuzione), è necessario chiamare prima il CreateWebPart metodo per creare il controllo e quindi chiamare il AddWebPart metodo per aggiungere controllo.To add a control that is not a WebPart control (in other words, a server control that will be wrapped with a GenericWebPart control at run time), you should first call the CreateWebPart method to create the control, and then call the AddWebPart method to add the control. Per una dimostrazione di questo approccio, vedere la sezione relativa agli esempi.For a demonstration of this approach, see the Example section.

Si applica a

Vedi anche