10.3.3 创建静态连接
还可以使用ASPX文件中的标记在两个Web Part之间创建连接,这些连接会为所有的用户保存下来,一般在把控件放在页面的过程中创建。这些连接也显示在WebPartManager的Connections集合中,允许在页面的代码中管理它们。
要创建静态连接,首先需要在WebPartManager标记中添加StaticConnections元素。在StaticConnections元素中,为每个要建立的连接放置一个Connection标记。每个标记都必须有三个特性。
● id:标记的唯一名称
● ProviderId:连接中提供程序Web Part的ID特性
● ConsumerId:连接中使用程序Web Part的ID特性
在前面的代码示例中,在提供程序和使用程序之间创建了连接的标记。
<asp:WebPartManager ID="WebPartManager1" runat="server">
<StaticConnections>
<asp:Connection ID="cn1" ProviderID="FindBook1"
ConsumerID="DisplayBook1" />
</StaticConnections>
</asp:WebPartManager>
这个示例假定,没有在ConnectionConsumer或ConnectionProvider特性中给连接点指定名称。如果指定了名称,就需要使用ProviderConnectionPointID或ConsumerConnectionPointID特性在Connection标记中指定这些名称。这个示例可用于有一个连接点的提供程序BookInfo(与第10.2.6节中的示例相同)。
<asp:WebPartManager ID="WebPartManager1" runat="server">
<StaticConnections>
<asp:Connection ID="cn1" ProviderID="FindBook1"
ConsumerID="DisplayBook1"
ProviderConnectionPointID="BookInfo"/>
</StaticConnections>
</asp:WebPartManager>
提示:
如果没有设置*ConnectionPointID特性,连接就会查找名为default的连接点,这是连接点的默认名称。如果没有找到合适的连接点,在请求页面时会生成一个错误。
注意:
对于连接两个Web Part生成的错误,可以从WebPart对象的ConnectErrorMessage中获取错误消息,在Visual Basic 2005中使用Me.ConnectErrorMessage,在C#中使用this.ConnectErrorMessage。
如果把Web Part放在Master页面上,但希望在Content页面上创建静态连接(可能要在不同的页面上创建不同的连接),就需要使用ProxyWebPartManager。把ProxyWebPartManager拖放到Content页面的内容块上,切换到Source视图,在ProxyWebPartManager的开闭标记中插入静态连接的标记即可。下面是一个典型的例子。
<asp:ProxyWebPartManager ID="ProxyWebPartManager1" runat="server">
<StaticConnections>
<asp:Connection ID="cn1" ProviderID="FindBook1"
ConsumerID="DisplayBook1"
ProviderConnectionPointID="BookInfo"/>
</StaticConnections>
</asp:ProxyWebPartManager>






