WebPartConnection.IsActive 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取一个值,该值指示 WebPartConnection 对象当前是否已经建立并且能够在其提供者和使用者控件之间交换数据。
public:
property bool IsActive { bool get(); };
[System.ComponentModel.Browsable(false)]
public bool IsActive { get; }
[<System.ComponentModel.Browsable(false)>]
member this.IsActive : bool
Public ReadOnly Property IsActive As Boolean
属性值
如果连接处于活动状态,则为 true
;否则为 false
。
- 属性
示例
下面的代码示例演示如何使用 IsActive 属性。
该示例包含三个部分:
接口和两 WebPart 个控件的源代码,充当连接的提供程序和使用者。
用于承载所有控件并运行代码示例的网页。
有关如何运行示例页的说明。
代码示例的第一部分是 接口的源代码,以及使用者和提供程序控件。 若要运行代码示例,必须编译此源代码。 可以显式编译它,并将生成的程序集放入网站的 Bin 文件夹或全局程序集缓存中。 或者,可以将源代码放在站点的“App_Code”文件夹中,并在运行时对其进行动态编译。 此代码示例使用动态编译。 有关演示如何编译的演练,请参阅 演练:开发和使用自定义 Web 服务器控件。
namespace Samples.AspNet.CS.Controls
{
using System;
using System.Web;
using System.Web.Security;
using System.Security.Permissions;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
[AspNetHostingPermission(SecurityAction.Demand,
Level = AspNetHostingPermissionLevel.Minimal)]
[AspNetHostingPermission(SecurityAction.InheritanceDemand,
Level = AspNetHostingPermissionLevel.Minimal)]
public interface IZipCode
{
string ZipCode { get; set;}
}
[AspNetHostingPermission(SecurityAction.Demand,
Level = AspNetHostingPermissionLevel.Minimal)]
[AspNetHostingPermission(SecurityAction.InheritanceDemand,
Level = AspNetHostingPermissionLevel.Minimal)]
public class ZipCodeWebPart : WebPart, IZipCode
{
string zipCodeText = String.Empty;
TextBox input;
Button send;
public ZipCodeWebPart()
{
}
// Make the implemented property personalizable to save
// the Zip Code between browser sessions.
[Personalizable()]
public virtual string ZipCode
{
get { return zipCodeText; }
set { zipCodeText = value; }
}
// This is the callback method that returns the provider.
[ConnectionProvider("Zip Code Provider", "ZipCodeProvider")]
public IZipCode ProvideIZipCode()
{
return this;
}
protected override void CreateChildControls()
{
Controls.Clear();
input = new TextBox();
this.Controls.Add(input);
send = new Button();
send.Text = "Enter 5-digit Zip Code";
send.Click += new EventHandler(this.submit_Click);
this.Controls.Add(send);
}
private void submit_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(input.Text))
{
zipCodeText = Page.Server.HtmlEncode(input.Text);
input.Text = String.Empty;
}
}
}
[AspNetHostingPermission(SecurityAction.Demand,
Level = AspNetHostingPermissionLevel.Minimal)]
[AspNetHostingPermission(SecurityAction.InheritanceDemand,
Level = AspNetHostingPermissionLevel.Minimal)]
public class WeatherWebPart : WebPart
{
private IZipCode _provider;
string _zipSearch;
Label DisplayContent;
// This method is identified by the ConnectionConsumer
// attribute, and is the mechanism for connecting with
// the provider.
[ConnectionConsumer("Zip Code Consumer", "ZipCodeConsumer")]
public void GetIZipCode(IZipCode Provider)
{
_provider = Provider;
}
protected override void OnPreRender(EventArgs e)
{
EnsureChildControls();
if (this._provider != null)
{
_zipSearch = _provider.ZipCode.Trim();
DisplayContent.Text = "My Zip Code is: " + _zipSearch;
}
}
protected override void CreateChildControls()
{
Controls.Clear();
DisplayContent = new Label();
this.Controls.Add(DisplayContent);
}
}
}
Imports System.Web
Imports System.Web.Security
Imports System.Security.Permissions
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Namespace Samples.AspNet.VB.Controls
<AspNetHostingPermission(SecurityAction.Demand, _
Level:=AspNetHostingPermissionLevel.Minimal)> _
<AspNetHostingPermission(SecurityAction.InheritanceDemand, _
Level:=AspNetHostingPermissionLevel.Minimal)> _
Public Interface IZipCode
Property ZipCode() As String
End Interface
<AspNetHostingPermission(SecurityAction.Demand, _
Level:=AspNetHostingPermissionLevel.Minimal)> _
<AspNetHostingPermission(SecurityAction.InheritanceDemand, _
Level:=AspNetHostingPermissionLevel.Minimal)> _
Public Class ZipCodeWebPart
Inherits WebPart
Implements IZipCode
Private zipCodeText As String = String.Empty
Private input As TextBox
Private send As Button
Public Sub New()
End Sub
' Make the implemented property personalizable to save
' the Zip Code between browser sessions.
<Personalizable()> _
Public Property ZipCode() As String _
Implements IZipCode.ZipCode
Get
Return zipCodeText
End Get
Set(ByVal value As String)
zipCodeText = value
End Set
End Property
' This is the callback method that returns the provider.
<ConnectionProvider("Zip Code Provider", "ZipCodeProvider")> _
Public Function ProvideIZipCode() As IZipCode
Return Me
End Function
Protected Overrides Sub CreateChildControls()
Controls.Clear()
input = New TextBox()
Me.Controls.Add(input)
send = New Button()
send.Text = "Enter 5-digit Zip Code"
AddHandler send.Click, AddressOf Me.submit_Click
Me.Controls.Add(send)
End Sub
Private Sub submit_Click(ByVal sender As Object, _
ByVal e As EventArgs)
If input.Text <> String.Empty Then
zipCodeText = Page.Server.HtmlEncode(input.Text)
input.Text = String.Empty
End If
End Sub
End Class
<AspNetHostingPermission(SecurityAction.Demand, _
Level:=AspNetHostingPermissionLevel.Minimal)> _
<AspNetHostingPermission(SecurityAction.InheritanceDemand, _
Level:=AspNetHostingPermissionLevel.Minimal)> _
Public Class WeatherWebPart
Inherits WebPart
Private _provider As IZipCode
Private _zipSearch As String
Private DisplayContent As Label
' This method is identified by the ConnectionConsumer
' attribute, and is the mechanism for connecting with
' the provider.
<ConnectionConsumer("Zip Code Consumer", "ZipCodeConsumer")> _
Public Sub GetIZipCode(ByVal Provider As IZipCode)
_provider = Provider
End Sub
Protected Overrides Sub OnPreRender(ByVal e As EventArgs)
EnsureChildControls()
If Not (Me._provider Is Nothing) Then
_zipSearch = _provider.ZipCode.Trim()
DisplayContent.Text = "My Zip Code is: " + _zipSearch
End If
End Sub
Protected Overrides Sub CreateChildControls()
Controls.Clear()
DisplayContent = New Label()
Me.Controls.Add(DisplayContent)
End Sub
End Class
End Namespace
代码示例的第二部分是网页。 顶部附近是一个 指令,它引用两个 Register
动态编译 WebPart 控件的源代码。 静态连接在页面上的 元素中 <StaticConnections>
声明。 元素中有 <script>
四个事件处理程序。 每个事件处理程序检查静态连接上的 属性的值 IsActive ,并将一条消息写入控件, Label 指示连接在页面和控件生命周期的该状态是活动还是非活动。 这演示了连接在什么时间点变为活动状态,并在呈现页面后保持活动状态。
<%@ Page Language="C#" %>
<%@ register tagprefix="aspSample"
namespace="Samples.AspNet.CS.Controls" %>
<!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)
{
WebPartConnection conn = mgr.StaticConnections[0];
if (conn.IsActive)
lbl1.Text += "<em>Connection 0 is active.</em>";
else
lbl1.Text += "Connection 0 is inactive.";
}
protected void mgr_ConnectionsActivated(object sender, EventArgs e)
{
if(mgr.Connections[0].IsActive)
lbl2.Text += "<em>Connection 0 is active.</em>";
else
lbl2.Text += "Connection 0 is inactive.";
}
protected void mgr_ConnectionsActivating(object sender, EventArgs e)
{
if (mgr.Connections[0].IsActive)
lbl3.Text += "<em>Connection 0 is active.</em>";
else
lbl3.Text += "Connection 0 is inactive.";
}
protected void Page_PreRender(object sender, EventArgs e)
{
if (mgr.Connections[0].IsActive)
lbl4.Text += "<em>Connection 0 is active.</em>";
else
lbl4.Text += "Connection 0 is inactive.";
}
</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"
onconnectionsactivated="mgr_ConnectionsActivated"
onconnectionsactivating="mgr_ConnectionsActivating">
<StaticConnections>
<asp:WebPartConnection ID="conn1"
ConsumerConnectionPointID="ZipCodeConsumer"
ConsumerID="weather1"
ProviderConnectionPointID="ZipCodeProvider"
ProviderID="zip1" />
</StaticConnections>
</asp:WebPartManager>
<asp:WebPartZone ID="WebPartZone1" runat="server">
<ZoneTemplate>
<aspSample:ZipCodeWebPart ID="zip1" runat="server"
Title="Zip Code Provider" />
<aspSample:WeatherWebPart ID="weather1" runat="server"
Title="Zip Code Consumer" />
</ZoneTemplate>
</asp:WebPartZone>
<asp:ConnectionsZone ID="ConnectionsZone1" runat="server">
</asp:ConnectionsZone>
<asp:Button ID="Button1" runat="server"
Text="Connection Details"
OnClick="Button1_Click" />
<br />
<asp:Label ID="lbl1" runat="server">
<h3>Button_Click Status</h3>
</asp:Label>
<br />
<asp:Label ID="lbl2" runat="server">
<h3>ConnectionActivating Status</h3>
</asp:Label>
<br />
<asp:Label ID="lbl3" runat="server">
<h3>ConnectionActivated Status</h3>
</asp:Label>
<br />
<asp:Label ID="lbl4" runat="server">
<h3>ConnectionActivated Status</h3>
</asp:Label>
</div>
</form>
</body>
</html>
<%@ Page Language="VB" %>
<%@ Register TagPrefix="aspSample"
Namespace="Samples.AspNet.VB.Controls" %>
<!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)
Dim conn As WebPartConnection = mgr.StaticConnections(0)
If conn.IsActive Then
lbl1.Text += "<em>Connection 0 is active.</em>"
Else
lbl1.Text += "Connection 0 is inactive."
End If
End Sub
Protected Sub mgr_ConnectionsActivated(ByVal sender As Object, _
ByVal e As System.EventArgs)
If mgr.Connections(0).IsActive Then
lbl2.Text += "<em>Connection 0 is active.</em>"
Else
lbl2.Text += "Connection 0 is inactive."
End If
End Sub
Protected Sub mgr_ConnectionsActivating(ByVal sender As Object, _
ByVal e As System.EventArgs)
If mgr.Connections(0).IsActive Then
lbl3.Text += "<em>Connection 0 is active.</em>"
Else
lbl3.Text += "Connection 0 is inactive."
End If
End Sub
Protected Sub Page_PreRender(ByVal sender As Object, _
ByVal e As System.EventArgs)
If mgr.Connections(0).IsActive Then
lbl4.Text += "<em>Connection 0 is active.</em>"
Else
lbl4.Text += "Connection 0 is inactive."
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"
OnConnectionsActivated="mgr_ConnectionsActivated"
OnConnectionsActivating="mgr_ConnectionsActivating">
<StaticConnections>
<asp:WebPartConnection ID="conn1"
ConsumerConnectionPointID="ZipCodeConsumer"
ConsumerID="weather1"
ProviderConnectionPointID="ZipCodeProvider"
ProviderID="zip1" />
</StaticConnections>
</asp:WebPartManager>
<asp:WebPartZone ID="WebPartZone1" runat="server">
<ZoneTemplate>
<aspSample:ZipCodeWebPart ID="zip1" runat="server"
Title="Zip Code Provider" />
<aspSample:WeatherWebPart ID="weather1" runat="server"
Title="Zip Code Consumer" />
</ZoneTemplate>
</asp:WebPartZone>
<asp:ConnectionsZone ID="ConnectionsZone1" runat="server">
</asp:ConnectionsZone>
<asp:Button ID="Button1" runat="server"
Text="Connection Details"
OnClick="Button1_Click" />
<br />
<asp:Label ID="lbl1" runat="server">
<h3>Button_Click Status</h3>
</asp:Label>
<br />
<asp:Label ID="lbl2" runat="server">
<h3>ConnectionActivating Status</h3>
</asp:Label>
<br />
<asp:Label ID="lbl3" runat="server">
<h3>ConnectionActivated Status</h3>
</asp:Label>
<br />
<asp:Label ID="lbl4" runat="server">
<h3>ConnectionActivated Status</h3>
</asp:Label>
</div>
</form>
</body>
</html>
在浏览器中加载页面。 已创建静态连接,并且消息已写入标签,显示连接是否在页面和控件生命周期的各个点处于活动状态。 单击“ 连接详细信息” 按钮,请注意,连接在此时不处于活动状态,但每次在事件发生后 ConnectionsActivated 都会重新激活连接,并且该连接在 (仍然处于活动状态,并且将在页面事件发生后 PreRender 保持) 。
注解
属性 IsActive 指示 对象的状态 WebPartConnection 。 当连接处于此状态时,连接中的提供程序和使用者控件将进行通信,并且能够通过公共接口或 WebPartTransformer 对象交换数据。
当用户在正常浏览模式下查看包含已建立连接的呈现页面时,连接通常处于活动状态 (除非在页面加载) 时由于某些冲突或其他问题而无法激活。 在页面和控件生命周期的早期阶段,属性值为 false
。 在控件上WebPartManager引发 事件后ConnectionsActivated,将立即激活连接。 具体而言,连接是在使用者从提供程序或 对象检索指定接口的实例后激活的 WebPartTransformer 。
在由于页面上的多个连接而可能存在冲突或同步问题的情况下,了解连接是否处于活动状态非常有用。 例如,如果两个连接之间存在某种冲突,则 WebPartManager 控件可以选择不激活其中一个连接以避免冲突。