共用方式為


WebPartConnection.IsActive 屬性

定義

取得指出目前是否已建立 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>

在瀏覽器中載入頁面。 靜態連接已經建立,而且訊息已經寫入標籤,其中顯示連接是否在頁面和控件生命週期的各個點作用中。 按兩下 [連線詳細數據] 按鈕,並請注意,該連接在該時間點未作用中,但每次事件之後都會重新啟用連線,而且它仍在作用中 (,而且會在頁面的事件之後ConnectionsActivatedPreRender保持) 。

備註

屬性 IsActive 表示 物件的狀態 WebPartConnection 。 當連線處於此狀態時,連線中的提供者和取用者控件會通訊,而且能夠透過通用介面或 WebPartTransformer 物件交換數據。

當使用者在一般流覽模式中檢視包含已建立連線的轉譯頁面時,除非因為頁面載入) 時發生衝突或其他問題而無法啟用連線,否則連線通常會作用中 (。 在頁面和控件生命週期的早期階段中,屬性值為 false。 連接會在引發控制項上的 WebPartManager 事件之後ConnectionsActivated啟動。 具體而言,取用者從提供者或 WebPartTransformer 物件擷取指定介面的實例之後,就會啟動連接。

在發生衝突或同步處理問題時,瞭解連線是否為作用中,因為頁面上有多個連線而發生衝突或同步處理問題。 例如,如果兩個連線之間有某種衝突, WebPartManager 控件可以選擇不啟用其中一個連線以避免衝突。

適用於

另請參閱