WebPartManager.StaticConnections 属性
定义
获取对网页上所有定义为静态连接的 WebPartConnection 对象的集合的引用。Gets a reference to the collection of all WebPartConnection objects on a Web page that are defined as static connections.
public:
property System::Web::UI::WebControls::WebParts::WebPartConnectionCollection ^ StaticConnections { System::Web::UI::WebControls::WebParts::WebPartConnectionCollection ^ get(); };
[System.Web.UI.PersistenceMode(System.Web.UI.PersistenceMode.InnerProperty)]
public System.Web.UI.WebControls.WebParts.WebPartConnectionCollection StaticConnections { get; }
[<System.Web.UI.PersistenceMode(System.Web.UI.PersistenceMode.InnerProperty)>]
member this.StaticConnections : System.Web.UI.WebControls.WebParts.WebPartConnectionCollection
Public ReadOnly Property StaticConnections As WebPartConnectionCollection
属性值
一个 WebPartConnectionCollection,包含页上的所有静态连接。A WebPartConnectionCollection that contains all the static connections on the page.
- 属性
示例
下面的代码示例演示如何以编程方式使用 StaticConnections 属性。The following code example demonstrates programmatic use of the StaticConnections property.
此代码示例包含四个部分:The code example has four parts:
使您能够在 Web 部件页上更改显示模式的用户控件。A user control that enables you to change display modes on a Web Parts page.
包含两个自定义 WebPart 控件和自定义接口的源代码文件。A source code file that contains two custom WebPart controls and a custom interface.
一个网页,其中包含两个 WebPart 可以连接的自定义控件和一个
<asp:webpartmanager>元素。A Web page that contains two custom WebPart controls that can be connected, and an<asp:webpartmanager>element.说明如何在浏览器中工作。An explanation of how the example works in a browser.
下面的代码仅包含示例的网页部分。The following code contains only the Web page portion of the example. 还需要从类概述的 "示例" 部分获取示例的前两个部分,即自定义用户控件和自定义控件和接口的源代码 WebPartManager 。You will also need to obtain the first two parts of the example--the custom user control and the source code for the custom controls and interface--from the Example section of the WebPartManager class overview. 本主题还介绍了用于编译控件的选项 WebPart 。That topic also explains your options for compiling the WebPart controls.
此代码示例的第三部分是网页。The third part of the code example is the Web page. 页的声明性标记包含 Register 用户控件和自定义控件的指令。The declarative markup for the page contains Register directives for both the user control and the custom controls. 有一个 <asp:webpartmanager> 元素、一个 <asp:webpartzone> 元素包含自定义控件和一个 <asp:connectionszone> 元素。There is an <asp:webpartmanager> element, an <asp:webpartzone> element to contain the custom controls, and an <asp:connectionszone> element. 请注意,在 Page_Load 方法中,代码会检查连接是否已存在,如果不存在,则定义一个提供程序、一个使用者及其各自的连接点,然后将新连接添加到属性引用的静态连接集 StaticConnections 。Notice that in the Page_Load method, the code checks to see whether a connection already exists and, if not, defines a provider, a consumer, and their respective connection points, and then adds a new connection to the set of static connections referenced by the StaticConnections property.
<%@ Page Language="C#" %>
<%@ register TagPrefix="uc1"
TagName="DisplayModeMenuCS"
Src="DisplayModeMenuCS.ascx" %>
<%@ register tagprefix="aspSample"
Namespace="Samples.AspNet.CS.Controls"
Assembly="ConnectionSampleCS"%>
<!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 Page_Load(object sender, EventArgs e)
{
// Define provider, consumer, and connection points.
WebPart provider = mgr.WebParts["zip1"];
ProviderConnectionPoint provConnPoint =
mgr.GetProviderConnectionPoints(provider)["ZipCodeProvider"];
WebPart consumer = mgr.WebParts["weather1"];
ConsumerConnectionPoint consConnPoint =
mgr.GetConsumerConnectionPoints(consumer)["ZipCodeConsumer"];
// Check whether the connection already exists.
if (mgr.CanConnectWebParts(provider, provConnPoint,
consumer, consConnPoint))
{
// Create a new static connection.
WebPartConnection conn = new WebPartConnection();
conn.ID = "staticConn1";
conn.ConsumerID = "weather1";
conn.ConsumerConnectionPointID = "ZipCodeConsumer";
conn.ProviderID = "zip1";
conn.ProviderConnectionPointID = "ZipCodeProvider";
mgr.StaticConnections.Add(conn);
}
}
</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="mgr" runat="server" />
<div>
<uc1:DisplayModeMenuCS ID="displaymode1"
runat="server" />
<!-- Reference consumer and provider controls
in a zone. -->
<asp:WebPartZone ID="WebPartZone1" runat="server">
<ZoneTemplate>
<aspSample:ZipCodeWebPart ID="zip1"
runat="server"
Title="Zip Code Control"/>
<aspSample:WeatherWebPart ID="weather1"
runat="server"
Title="Weather Control" />
</ZoneTemplate>
</asp:WebPartZone>
<hr />
<!-- Add a ConnectionsZone so users can connect
controls. -->
<asp:ConnectionsZone ID="ConnectionsZone1"
runat="server" />
</div>
</form>
</body>
</html>
<%@ Page Language="vb" %>
<%@ register TagPrefix="uc1"
TagName="DisplayModeMenuVB"
Src="DisplayModeMenuVB.ascx" %>
<%@ register tagprefix="aspSample"
Namespace="Samples.AspNet.VB.Controls"
Assembly="ConnectionSampleVB"%>
<!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 Page_Load(ByVal sender As Object, _
ByVal e As System.EventArgs)
' Define provider, consumer, and connection points.
Dim provider As WebPart = mgr.WebParts("zip1")
Dim provConnPoint As ProviderConnectionPoint = _
mgr.GetProviderConnectionPoints(provider)("ZipCodeProvider")
Dim consumer As WebPart = mgr.WebParts("weather1")
Dim consConnPoint As ConsumerConnectionPoint = _
mgr.GetConsumerConnectionPoints(consumer)("ZipCodeConsumer")
' Check whether the connection already exists.
If mgr.CanConnectWebParts(provider, provConnPoint, _
consumer, consConnPoint) Then
' Create a new static connection.
Dim conn As New WebPartConnection()
conn.ID = "staticConn1"
conn.ConsumerID = "weather1"
conn.ConsumerConnectionPointID = "ZipCodeConsumer"
conn.ProviderID = "zip1"
conn.ProviderConnectionPointID = "ZipCodeProvider"
mgr.StaticConnections.Add(conn)
End If
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="mgr" runat="server" />
<div>
<uc1:DisplayModeMenuVB ID="displaymode1"
runat="server" />
<!-- Reference consumer and provider controls
in a zone. -->
<asp:WebPartZone ID="WebPartZone1" runat="server">
<ZoneTemplate>
<aspSample:ZipCodeWebPart ID="zip1"
runat="server"
Title="Zip Code Control"/>
<aspSample:WeatherWebPart ID="weather1"
runat="server"
Title="Weather Control" />
</ZoneTemplate>
</asp:WebPartZone>
<hr />
<!-- Add a ConnectionsZone so users can connect
controls. -->
<asp:ConnectionsZone ID="ConnectionsZone1"
runat="server" />
</div>
</form>
</body>
</html>
在浏览器中加载网页后,请单击 " 显示模式 " 下拉列表控件,然后选择 " 连接 ",将页面切换到连接模式。After you have loaded the Web page in a browser, click the Display Mode drop-down list control and select Connect to switch the page to connect mode. 连接模式使用 <asp:connectionszone> 元素使您能够在控件之间创建连接。Connect mode uses the <asp:connectionszone> element to enable you to create connections between controls. 在连接模式下,单击 邮政编码 控件标题栏中的向下箭头以激活其谓词菜单,然后单击 " 连接"。In connect mode, click the downward arrow in the title bar of the ZIP Code control to activate its verbs menu, and then click Connect. 连接用户界面 (UI) 出现后,请注意该方法中包含的代码已创建连接 Page_Load 。After the connection user interface (UI) appears, notice that a connection has already been created by the code contained in the Page_Load method. 如果在后续浏览器会话中返回到此页,则已建立此静态连接,并且每次加载页面时都不需要重新创建此静态连接。If you return to this page in a later browser session, this static connection will already be established and will not need to be recreated each time the page loads.
注解
StaticConnections控件使用属性 WebPartManager 来跟踪和管理页上的所有静态连接。The StaticConnections property is used by the WebPartManager control to track and manage all static connections on a page. 与动态连接相比,静态连接无需在每次呈现页面时添加到页面中。A static connection, in contrast with a dynamic connection, does not need to be added to a page every time the page is rendered.
此属性引用的集合包含页上存在的所有静态连接,无论它们是以编程方式创建,还是通过 <asp:webpartconnection> 页标记中的元素指定的。The collection referenced by this property contains all the static connections that exist on the page, whether they are created programmatically or specified with an <asp:webpartconnection> element in the page's markup.