WebPartManager.GetGenericWebPart(Control) Método

Definição

Obtém uma referência à instância do controle GenericWebPart que contém um controle de servidor.Gets a reference to the instance of the GenericWebPart control that contains a server control.

public:
 System::Web::UI::WebControls::WebParts::GenericWebPart ^ GetGenericWebPart(System::Web::UI::Control ^ control);
public System.Web.UI.WebControls.WebParts.GenericWebPart GetGenericWebPart (System.Web.UI.Control control);
member this.GetGenericWebPart : System.Web.UI.Control -> System.Web.UI.WebControls.WebParts.GenericWebPart
Public Function GetGenericWebPart (control As Control) As GenericWebPart

Parâmetros

control
Control

Um controle de servidor que existe em um WebPartZoneBase e é encapsulado como um controle filho de um GenericWebPart em tempo de execução.A server control that exists in a WebPartZoneBase and is wrapped as a child control of a GenericWebPart at run time.

Retornos

GenericWebPart

Um GenericWebPart que encapsula control como um controle filho.A GenericWebPart that wraps control as a child control. O método retorna null se control não está contido em um GenericWebPart.The method returns null if control is not contained in a GenericWebPart.

Exceções

control é null.control is null.

Exemplos

O exemplo de código a seguir demonstra o uso do GetGenericWebPart método.The following code example demonstrates the use of the GetGenericWebPart method. O exemplo de código contém um Calendar controle declarado em uma WebPartZone zona.The code example contains a Calendar control declared within a WebPartZone zone. O Button1_Click método primeiro imprime a ID do Calendar controle em um rótulo e, em seguida, usa o GetGenericWebPart método para recuperar uma referência ao GenericWebPart controle que encapsula o calendário.The Button1_Click method first prints the ID of the Calendar control to a label, and then uses the GetGenericWebPart method to retrieve a reference to the GenericWebPart control that wraps the calendar. A ID do GenericWebPart controle e a ID de seu controle filho (que é o Calendar controle) são impressos em um segundo rótulo.The ID of the GenericWebPart control, and the ID of its child control (which is the Calendar control), are both printed to a second label.

<%@ 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 Button1_Click(object sender, EventArgs e)
  {
    Label1.Text = "<h2>Server Control</h2>";
    Label1.Text += "Server Control ID:  " + Calendar1.ID;
    Label2.Text = "<h2>GenericWebPart Control</h2>";
    GenericWebPart part = mgr.GetGenericWebPart(Calendar1);
    if (part != null)
    {
      Label2.Text +=
        "GenericWebPart ID:  " + part.ID + "<br />";
      Label2.Text +=
        "Underlying Control ID: " + part.ChildControl.ID;
    }
  }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      <asp:WebPartManager ID="mgr" runat="server" />
      <asp:WebPartZone ID="WebPartZone1" runat="server">
        <ZoneTemplate>
          <asp:Calendar ID="Calendar1" runat="server"
            Title="My Calendar" />
        </ZoneTemplate>
      </asp:WebPartZone>
      <asp:Button ID="Button1" runat="server" 
        Text="Get GenericWebPart"
        OnClick="Button1_Click" />
      <hr />
      <asp:Label ID="Label1" runat="server" Text="" />
      <br />
      <asp:Label ID="Label2" runat="server" Text="" />
    </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 Button1_Click(ByVal sender As Object, _
    ByVal e As System.EventArgs)
    
    Label1.Text = "<h2>Server Control</h2>"
    Label1.Text += "Server Control ID:  " + Calendar1.ID
    Label2.Text = "<h2>GenericWebPart Controls</h2>"
    Dim part As GenericWebPart
    part = mgr.GetGenericWebPart(Calendar1)
    If part IsNot Nothing Then
      Label2.Text += _
        "GenericWebPart ID:  " & part.ID & "<br />"
      Label2.Text += _
        "Underlying Control ID: " + part.ChildControl.ID
    End If
    
  End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      <asp:WebPartManager ID="mgr" runat="server" />
      <asp:WebPartZone ID="WebPartZone1" runat="server">
        <ZoneTemplate>
          <asp:Calendar ID="Calendar1" runat="server"
            Title="My Calendar" />
        </ZoneTemplate>
      </asp:WebPartZone>
      <asp:Button ID="Button1" runat="server" 
        Text="Get GenericWebPart"
        OnClick="Button1_Click" />
      <hr />
      <asp:Label ID="Label1" runat="server" Text="" />
      <br />
      <asp:Label ID="Label2" runat="server" Text="" />
    </div>
    </form>
</body>
</html>

Comentários

Em geral, há duas categorias de controles que os desenvolvedores colocam em WebPartZoneBase zonas para participar de Web Parts aplicativos: WebPart controles, que herdam da WebPart classe base e outros controles de servidor, que podem ser controles ASP.NET padrão, controles personalizados ou controles de usuário.In general, there are two categories of controls that developers place in WebPartZoneBase zones to participate in Web Parts applications: WebPart controls, which inherit from the WebPart base class, and other server controls, which can be standard ASP.NET controls, custom controls, or user controls. Quando qualquer um desses controles é colocado em uma WebPartZoneBase zona, ele assume a funcionalidade de um WebPart controle.When any of these controls is placed in a WebPartZoneBase zone, it takes on the functionality of a WebPart control. Um WebPart controle tem essa funcionalidade inerentemente, mas os outros tipos de controles de servidor não.A WebPart control has this functionality inherently, but the other kinds of server controls do not. Para permitir que os outros controles de servidor atuem como WebPart controles quando são colocados em uma WebPartZoneBase zona, o ASP.net os encapsula com um GenericWebPart controle.To enable the other server controls to act as WebPart controls when they are placed in a WebPartZoneBase zone, ASP.NET wraps them with a GenericWebPart control. Como o GenericWebPart controle herda diretamente da WebPart classe, ele fornece seus controles filho com recursos verdadeiros de Web Parts.Because the GenericWebPart control inherits directly from the WebPart class, it provides its child controls with true Web Parts features.

Geralmente, em tempo de execução, os desenvolvedores de páginas podem querer obter uma referência ao GenericWebPart controle que contém um dos controles de servidor em uma zona.Often at run time, page developers might want to get a reference to the GenericWebPart control that contains one of the server controls in a zone. O GetGenericWebPart método permite que eles recuperem uma referência ao GenericWebPart controle.The GetGenericWebPart method enables them to retrieve a reference to the GenericWebPart control.

Aplica-se a

Confira também