WebPartManager.WebParts 属性

定义

获取对网页上的 WebPart 控件所跟踪的所有 WebPartManager 控件的引用。Gets a reference to all WebPart controls tracked by the WebPartManager control on a Web page.

public:
 property System::Web::UI::WebControls::WebParts::WebPartCollection ^ WebParts { System::Web::UI::WebControls::WebParts::WebPartCollection ^ get(); };
[System.ComponentModel.Browsable(false)]
public System.Web.UI.WebControls.WebParts.WebPartCollection WebParts { get; }
member this.WebParts : System.Web.UI.WebControls.WebParts.WebPartCollection
Public ReadOnly Property WebParts As WebPartCollection

属性值

一个 WebPartCollection,包含对一组 WebPart 控件的引用。A WebPartCollection that contains references to a set of WebPart controls.

属性

示例

下面的代码示例演示如何以编程方式使用 WebParts 属性访问单个 WebPart 控件。The following code example demonstrates how to use the WebParts property programmatically to access individual WebPart controls. 请注意,在网页的声明性标记中,在 <asp:webpartzone> 元素中有两个标准 ASP.NET 服务器控件。Notice that in the declarative markup for the Web page, within the <asp:webpartzone> element there are two standard ASP.NET server controls. 尽管这些不从 WebPart 类继承,但它们在区域中,它们会在运行时使用 GenericWebPart 对象进行包装,因此将包含在 WebParts 属性所引用的集合中。Although these do not inherit from the WebPart class, because they are in a zone they will be wrapped with a GenericWebPart object at run time, and thus will be included in the collection referenced by the WebParts property. 你还可以在此区域中添加自定义 WebPart 控件、用户控件或自定义服务器控件,并以相同的方式处理它们。You could also add custom WebPart controls, user controls, or custom server controls in this zone, and they would be handled the same way.

<%@ 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 = "WebPart count:  " +
      WebPartManager1.WebParts.Count.ToString();
  }

  protected void Button2_Click(object sender, EventArgs e)
  {
    WebPartManager1.WebParts["Calendar1"].ChromeType = 
      PartChromeType.BorderOnly;
  }
</script>

<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">
      <!-- Reference the WebPartManager control. -->
    <asp:WebPartManager ID="WebPartManager1" runat="server" />    
    <div>
      <asp:WebPartZone ID="WebPartZone1" runat="server">
        <ZoneTemplate>
          <asp:BulletedList 
            DisplayMode="HyperLink" 
            ID="BulletedList1" 
            runat="server"
            Title="My Links"
            ExportMode="All">
            <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>
          <asp:Calendar ID="Calendar1" runat="server" 
            Title="My Calendar" />
        </ZoneTemplate>
      </asp:WebPartZone>
      <hr />
      <asp:Button ID="Button1" runat="server" 
        Text="WebPart Count" 
        OnClick="Button1_Click" />
      <asp:Button ID="Button2" runat="server" 
        Text="Hide Calendar Title" 
        OnClick="Button2_Click" />  
      <br />
      <asp:Label ID="Label1" 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 = "WebPart count:  " & _
      WebPartManager1.WebParts.Count.ToString()
  End Sub

  Protected Sub Button2_Click(ByVal sender As Object, _
    ByVal e As System.EventArgs)
    
    WebPartManager1.WebParts("Calendar1").ChromeType = _
      PartChromeType.BorderOnly

  End Sub
</script>

<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">
      <!-- Reference the WebPartManager control. -->
    <asp:WebPartManager ID="WebPartManager1" runat="server" />    
    <div>
      <asp:WebPartZone ID="WebPartZone1" runat="server">
        <ZoneTemplate>
          <asp:BulletedList 
            DisplayMode="HyperLink" 
            ID="BulletedList1" 
            runat="server"
            Title="My Links"
            ExportMode="All">
            <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>
          <asp:Calendar ID="Calendar1" runat="server" 
            Title="My Calendar" />
        </ZoneTemplate>
      </asp:WebPartZone>
      <hr />
      <asp:Button ID="Button1" runat="server" 
        Text="WebPart Count" 
        OnClick="Button1_Click" />
      <asp:Button ID="Button2" runat="server" 
        Text="Hide Calendar Title" 
        OnClick="Button2_Click" />  
      <br />
      <asp:Label ID="Label1" runat="server" text="" />
    </div>
    </form>
</body>
</html>

请注意,若要使代码示例正常运行,必须在 web.config 文件中添加一个设置,以便导出 Web 部件说明文件。Note that for the code example to work, you must add a setting in the Web.config file to enable exporting Web Parts description files. 确保在此代码示例的网页所在的同一目录中有一个 web.config 文件。Ensure that you have a Web.config file in the same directory as the Web page for this code example. <system.web> 部分中,请确保存在一个 <webParts> 元素,其 enableExport 特性设置为 true,如以下标记所示。Within the <system.web> section, make sure there is a <webParts> element with an enableExport attribute set to true, as in the following markup.

<webParts enableExport="true">

...

</webParts>

将页面加载到浏览器后,如果单击 " Web 部件计数" 按钮,代码将使用 WebParts 属性返回集合中的控件计数。After you load the page into a browser, if you click the WebPart Count button, the code uses the WebParts property to return the count of controls in the collection. 如果单击 "隐藏日历标题" 按钮,则代码会更改日历,使其仅使用边框而不是标题进行呈现。If you click the Hide Calendar Title button, the code changes the calendar so that it renders with only a border and not a title.

注解

WebPartManager 控件使用 WebParts 属性来跟踪页面上 WebPartZoneBase 区域中包含的所有 WebPart 控件。The WebParts property is used by the WebPartManager control to track all WebPart controls that are contained within WebPartZoneBase zones on the page. 尽管该集合是只读的,但你可以通过集合访问单个 WebPart 控件并以编程方式对其进行更改。Although the collection is read-only, you can access individual WebPart controls through the collection and make changes to them programmatically.

备注

可以将 WebPart 控件放置在 WebPartZone 区域外的页上,这会导致该控件不会被 WebPartManager 控件跟踪或在其 WebParts 集合中被引用。It is possible to have a WebPart control placed on a page outside of a WebPartZone zone, which results in the control not being tracked by the WebPartManager control or referenced in its WebParts collection. 不过,几乎不需要在区域外使用 WebPart 控件,因为它会丢失其 Web 部件功能并作为正常的服务器控件。However, there is little reason to use a WebPart control outside of a zone, because it then loses its Web Parts functionality and acts as a normal server control.

可在区域中放置的任何类型的控件、自定义 WebPart 控件、标准 ASP.NET 控件、用户控件或自定义服务器控件是否可在运行时被视为 WebPart 控件。Any type of control that can be placed in a zone, whether a custom WebPart control, a standard ASP.NET control, a user control, or a custom server control, can be treated as a WebPart control at run time. 如果不是 WebPart 控件的控件放置在 WebPartZone 区域中,则在运行时 ASP.NET 使用 GenericWebPart 对象包装控件,使控件可以作为真正的 WebPart 控件。When a control that is not a WebPart control is placed in a WebPartZone zone, at run time ASP.NET wraps the control with a GenericWebPart object, so that the control can behave as a true WebPart control. 因此,通过使用 WebParts 属性,WebPartManager 控件可以跟踪任意类型的服务器控件,而不管它是否从 WebPart 类派生。Therefore, by using the WebParts property, the WebPartManager control can track any type of server control, regardless of whether it derives from the WebPart class.

适用于