ASP.NET - ConnectionsZone Web 服务器控件概述
来源:互联网 发布:dota淘宝交易 编辑:程序博客网 时间:2024/06/04 19:03
使用 Web 部件控件,可以让用户在运行时创建两个服务器控件之间的连接,以形成连接并共享数据。一个控件用作数据的提供者,另一个控件用作数据的使用者。这两个控件可以是 WebPart 控件或任何其他类型的服务器控件。必须将它们设计为处理连接且位于 WebPartZoneBase 区域中。
在配置控件以形成 Web 部件连接之后,仍需要连接这些控件。连接控件的方法有:
·在网页中声明连接。
·用代码创建连接。
·将 ConnectionsZone 控件添加到页中,以便用户可以根据需要连接控件。
ConnectionsZone 控件生成 UI,以便用户能够连接任何合格控件或断开此类连接。该控件为可选控件,它不是形成连接所必需的。不过,如果想赋予用户有关连接或断开服务器控件的控制权,该控件将很有用。
通过 ConnectionsZone 控件控制的 Web 部件控件属性
ConnectionsZone 控件提供的用户界面能让用户设置与指定的使用者控件和指定的提供者控件相关的 ConnectionPoint 对象。
启用 ConnectionsZone 控件
作为一个工具区域,ConnectionsZone 控件被设计为仅当网页处于连接模式时可见。(当页上的 WebPartManager 控件将其 DisplayMode 属性设置为 ConnectDisplayMode 时,该页将处于连接模式。) 用户将页切换到连接模式后,必须单击其中一个服务器控件的连接谓词才能显示连接 UI。
一、ConnectionsZone 类
提供一个用户界面 (UI),该界面使用户能够在位于 WebPartZoneBase 区域中的 WebPart 与其他服务器控件之间形成连接。
通过使用 Web 部件控件集,可以使两个服务器控件形成连接并共享数据,其中一个控件作为数据提供者,另一个控件作为数据使用者。这两个控件可以是 WebPart 控件或任何其他类型的服务器控件,条件是它们能够处理连接且位于 WebPartZoneBase 区域中。
即使存在形成 Web 部件连接所必需的控件和条件,仍然必须真正地连接控件。有三种方式在服务器控件之间形成连接:在网页中声明连接,在代码中创建连接,或是将 ConnectionsZone 控件添加到页面以便用户可以根据需要连接控件。ConnectionsZone 控件生成一个用户界面,该用户界面使用户能够连接页面上满足形成连接所需条件的任何服务器控件或是断开这些控件的连接。该控件为可选控件,不是形成连接所必需的,但在希望用户能够控制连接哪些控件或是断开哪些控件的连接的情况下,该控件十分有用。
ConnectionsZone 控件是继承自 ToolZone 基类的 Web 部件工具区域控件之一。作为工具区域,ConnectionsZone 控件被设计为仅当其网页处于特定显示模式时才可见。在这种情况下,显示模式称为连接模式(当页面上的 WebPartManager 控件的 DisplayMode 属性值设置为 ConnectDisplayMode 时,该页面处于此模式)。当用户将页面切换到连接模式后,必须在一个服务器控件的谓词菜单上单击连接谓词,然后连接用户界面变为可见。
作为 Web 部件区域控件,ConnectionsZone 控件是设计为包含其他控件的 WebZone 区域(该区域继承自 CompositeControl 类)的一种。一般来说,ConnectionsZone 区域具有的大多数元素与其他 Web 部件工具区域相同:页眉、正文或内容区域以及页脚。
重要说明: 注意,与大多数其他 Web 部件区域不同,ConnectionsZone 区域不包含与其相关联的唯一服务器控件类型。有关区域以及它们所包含的关联控件的列表,请参见 WebZone 类概述中的图表。但是 ConnectionsZone 区域不包含 WebPartConnection 控件。实际上,该区域很少用于为用户提供用户界面以连接页面上某个 WebPartZoneBase 区域中存在的服务器控件或断开这些控件的连接。ConnectionsZone 控件中所包含的控件仅为标准 ASP.NET 服务器控件,标准 ASP.NET 服务器控件是由 ConnectionsZone 控件作为用于形成连接的用户界面的一部分生成的。
呈现 ConnectionsZone 控件时,该控件根据页面上能够形成连接的服务器控件生成一个用户界面。ConnectionsZone 控件确定页面上 WebPartZoneBase 区域中的哪些服务器控件是提供者,哪些控件是使用者,哪些连接点可用,以及服务器控件当前是连接的还是断开连接的,然后相应地生成用户界面。
例如,假设有一个能够作为提供者的 WebPart 控件,一个能够作为使用者的 WebPart 控件,这两个控件在页面上的 WebPartZone 中声明,而且当前是断开连接的。当用户将页面切换到连接模式并在一个控件上单击连接谓词时,ConnectionsZone 控件生成一个包含一个链接的用户界面,该链接在单击时显示一个窗体,用户可以在该窗体中选择选项以创建连接。(如果这些控件以前是连接的,则初始视图改为向用户提供一个按钮以断开这些控件的连接)。在用于创建新连接的连接用户界面中,会向用户显示哪个控件为提供者,哪个控件为使用者。每个服务器控件下方都会显示一个下拉列表控件,列出控件的可用 ConnectionPoint 对象。用户必须从控件各自的下拉列表中为提供者选择一个 ProviderConnectionPoint 对象(以确定将与使用者共享的接口和数据),为将要连接到提供者的每个使用者选择一个 ConsumerConnectionPoint 对象(以确定该使用者将使用的接口和数据)。
说明: 在 Web 部件控件集默认实现中,一个提供者可以连接到多个使用者,但是一个使用者只能有一个提供者。
若要使用 ConnectionsZone 控件,可以使用 <asp:connectionszone> 元素在网页上的 <form> 元素中(但是不能嵌套在另一个 Web 部件区域元素中)声明该控件,也可以以编程方式将该控件添加到页面中。如果在页面中声明元素,则与其他 Web 部件区域不同,您不能在 <asp:connectionszone> 元素的标记之间声明任何其他类型的服务器控件。可以在该元素中声明与其自己的属性和样式详细信息相关的元素,但是该元素是独立的元素,不是可以在其中声明其他服务器控件的模板控件。
说明: 为改进可访问性,在 <fieldset> 元素中呈现了 ConnectionsZone 控件。<fieldset> 元素在 ConnectionsZone 控件中对一组相关的用于建立连接的控件进行分组,还可为在那些兼用于可视化用户代理(如普通 Web 浏览器)和面向语音的用户代理(如屏幕阅读软件)的控件之间进行选项卡式导航提供便利。
ConnectionsZone 控件具有多个用于呈现连接用户界面的属性。一个属性集包括多个在用户界面中执行操作的谓词(仅用于与连接相关的情况):ConfigureVerb、ConnectVerb 和 DisconnectVerb。大的属性集(尤其是用于连接区域用户界面的属性集)包含在用户界面中的各个位置(或在特定情况下,如出现错误时)显示的文本字符串:ConfigureConnectionTitle、ConnectToConsumerInstructionText、ConnectToConsumerText、ConnectToConsumerTitle、ConnectToProviderInstructionText、ConnectToProviderText、ConnectToProviderTitle、ConsumersInstructionText、ConsumersTitle、ExistingConnectionErrorMessage、GetFromText、GetText、InstructionTitle、NewConnectionErrorMessage、NoExistingConnectionInstructionText、NoExistingConnectionTitle、ProvidersInstructionText、ProvidersTitle、SendText 和 SendToText。ConnectionsZone 类还包含许多可在其他 Web 部件区域中找到的通用属性:CancelVerb、CloseVerb、Display、EmptyZoneText、HeaderText、InstructionText 和 PartChromeType。最后,WebPartToConnect 属性对于该类是唯一的,它引用启动连接的控件(即用户在其上单击谓词菜单中的连接谓词的控件,也是在 WebPartManager 控件的 SelectedWebPart 属性中引用的控件)。
ConnectionsZone 类还有许多方法(所有这些方法都从基类继承和重写而来),其中的大多数方法来自 Web 部件区域基类。
对继承者的说明:如果开发人员希望更改 ConnectionsZone 类的行为或该类为处理连接而提供的默认用户界面,则可以扩展该类。
1.1、 示例
下面的代码示例演示如何在 Web 部件页上使用 ConnectionsZone 控件。此示例包含四部分:
·一个用户控件,用于切换网页上的显示模式。
·一个源文件,包含邮政编码接口的代码和两个 WebPart 控件(用作连接的提供者和使用者)的代码。
·一个网页,该网页承载所有控件,演示如何声明 <asp:connectionszone> 元素,并以声明方式和编程方式设置连接区域的多个属性。
·关于该示例在浏览器中的工作方式的说明。
此代码示例的第一部分是一个用户控件,该控件使用户能够切换网页上的显示模式。
<%@ control language="C#" classname="DisplayModeMenuCS"%>
<script runat="server">
// Use a field to reference the current WebPartManager.
WebPartManager _manager;
void Page_Init(object sender, EventArgs e)
{
Page.InitComplete += new EventHandler(InitComplete);
}
void InitComplete(object sender, System.EventArgs e)
{
_manager = WebPartManager.GetCurrentWebPartManager(Page);
String browseModeName = WebPartManager.BrowseDisplayMode.Name;
// Fill the dropdown with the names of supported display modes.
foreach (WebPartDisplayMode mode in _manager.SupportedDisplayModes)
{
String modeName = mode.Name;
// Make sure a mode is enabled before adding it.
if (mode.IsEnabled(_manager))
{
ListItem item = new ListItem(modeName, modeName);
DisplayModeDropdown.Items.Add(item);
}
}
// If shared scope is allowed for this user, display the scope-switching
// UI and select the appropriate radio button for the current user scope.
if (_manager.Personalization.CanEnterSharedScope)
{
Panel2.Visible = true;
if (_manager.Personalization.Scope == PersonalizationScope.User)
RadioButton1.Checked = true;
else
RadioButton2.Checked = true;
}
}
// Change the page to the selected display mode.
void DisplayModeDropdown_SelectedIndexChanged(object sender, EventArgs e)
{
String selectedMode = DisplayModeDropdown.SelectedValue;
WebPartDisplayMode mode = _manager.SupportedDisplayModes[selectedMode];
if (mode != null)
_manager.DisplayMode = mode;
}
// Set the selected item equal to the current display mode.
void Page_PreRender(object sender, EventArgs e)
{
ListItemCollection items = DisplayModeDropdown.Items;
int selectedIndex =
items.IndexOf(items.FindByText(_manager.DisplayMode.Name));
DisplayModeDropdown.SelectedIndex = selectedIndex;
}
// Reset all of a user's personalization data for the page.
protected void LinkButton1_Click(object sender, EventArgs e)
{
_manager.Personalization.ResetPersonalizationState();
}
// If not in User personalization scope, toggle into it.
protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
{
if (_manager.Personalization.Scope == PersonalizationScope.Shared)
_manager.Personalization.ToggleScope();
}
// If not in Shared scope, and if user is allowed, toggle the scope.
protected void RadioButton2_CheckedChanged(object sender, EventArgs e)
{
if (_manager.Personalization.CanEnterSharedScope &&
_manager.Personalization.Scope == PersonalizationScope.User)
_manager.Personalization.ToggleScope();
}
</script>
<div>
<asp:Panel ID="Panel1" runat="server"
Borderwidth="1"
Width="230"
BackColor="lightgray"
Font-Names="Verdana, Arial, Sans Serif" >
<asp:Label ID="Label1" runat="server"
Text=" Display Mode"
Font-Bold="true"
Font-Size="8"
Width="120"
AssociatedControlID="DisplayModeDropdown"/>
<asp:DropDownList ID="DisplayModeDropdown" runat="server"
AutoPostBack="true"
Width="120"
OnSelectedIndexChanged="DisplayModeDropdown_SelectedIndexChanged" />
<asp:LinkButton ID="LinkButton1" runat="server"
Text="Reset User State"
ToolTip="Reset the current user's personalization data for the page."
Font-Size="8"
OnClick="LinkButton1_Click" />
<asp:Panel ID="Panel2" runat="server"
GroupingText="Personalization Scope"
Font-Bold="true"
Font-Size="8"
Visible="false" >
<asp:RadioButton ID="RadioButton1" runat="server"
Text="User"
AutoPostBack="true"
GroupName="Scope" OnCheckedChanged="RadioButton1_CheckedChanged" />
<asp:RadioButton ID="RadioButton2" runat="server"
Text="Shared"
AutoPostBack="true"
GroupName="Scope"
OnCheckedChanged="RadioButton2_CheckedChanged" />
</asp:Panel>
</asp:Panel>
</div>
该示例的第二部分是包含接口和自定义控件的源文件。注意,ZipCodeWebPart 控件实现了 IZipCode 接口,方法是添加 ConnectionProvider 属性以使该控件可以用作连接的提供者。WeatherWebPart 控件具有一个用 ConnectionConsumer 属性(该控件在其中使用 IZipCode 接口)标记的方法,因此该控件可以用作连接中的使用者。
必须编译此源代码,代码示例才能运行。可以显式编译源代码,并将结果程序集放在网站的 Bin 文件夹或全局程序集缓存中。另外,也可将源代码放入站点的 App_Code 文件夹中,源代码将在运行时在此文件夹中进行动态编译。此示例使用动态编译。
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 (input.Text != String.Empty)
{
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);
}
}
}
该代码示例的第三部分为网页。靠近顶部的是连接中使用的用户控件和自定义控件的 Register 指令。在页面中声明了一个 <asp:connectionszone> 元素,作为以声明方式使用 ConnectionsZone 控件的示例。在该元素中,以声明方式设置了多个属性。连接区域的其他属性是在页面的 <script> 部分中以编程方式设置的。
<%@ Page Language="C#" %>
<%@ register tagprefix="uc1"
tagname="DisplayModeMenuCS"
src="~/displaymodemenucs.ascx" %>
<%@ 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 Page_PreRender(object sender, EventArgs e)
{
// Set properties on verbs.
connectionsZone1.CancelVerb.Description =
"Terminates the connection process";
connectionsZone1.CloseVerb.Description =
"Closes the connections UI";
connectionsZone1.ConfigureVerb.Description =
"Configure the transformer for the connection";
connectionsZone1.ConnectVerb.Description =
"Connect two WebPart controls";
connectionsZone1.DisconnectVerb.Description =
"End the connection between two controls";
// Set properties for UI text strings.
connectionsZone1.ConfigureConnectionTitle =
"Configure";
connectionsZone1.ConnectToConsumerInstructionText =
"Choose a consumer connection point";
connectionsZone1.ConnectToConsumerText =
"Select a consumer for the provider to connect with";
connectionsZone1.ConnectToConsumerTitle =
"Send data to this consumer";
connectionsZone1.ConnectToProviderInstructionText =
"Choose a provider connection point";
connectionsZone1.ConnectToProviderText =
"Select a provider for the consumer to connect with";
connectionsZone1.ConnectToProviderTitle =
"Get data from this provider";
connectionsZone1.ConsumersInstructionText =
"WebPart controls that receive data from providers";
connectionsZone1.ConsumersTitle = "Consumer Controls";
connectionsZone1.GetFromText = "Receive from";
connectionsZone1.GetText = "Retrieve";
connectionsZone1.HeaderText =
"Create and Manage Connections";
connectionsZone1.InstructionText =
"Manage connections for the selected WebPart control";
connectionsZone1.InstructionTitle =
"Manage connections for consumers or providers";
connectionsZone1.NoExistingConnectionInstructionText =
"No connections exist. Click the above link to create "
+ "a connection.";
connectionsZone1.NoExistingConnectionTitle =
"No current connections";
connectionsZone1.ProvidersInstructionText =
"WebPart controls that send data to consumers";
connectionsZone1.ProvidersTitle = "Provider controls";
}
</script>
<html >
<head runat="server">
<title>Connection Zone Sample</title>
</head>
<body>
<form id="form1" runat="server">
<asp:webpartmanager runat="server" id="mgr">
<staticconnections>
<asp:webpartconnection id="connection1"
consumerconnectionpointid="ZipCodeConsumer"
consumerid="zipConsumer"
providerconnectionpointid="ZipCodeProvider"
providerid="zipProvider" />
</staticconnections>
</asp:webpartmanager>
<uc1:displaymodemenucs id="menu1" runat="server" />
<div>
<asp:webpartzone id="WebPartZone1" runat="server">
<zonetemplate>
<aspsample:zipcodewebpart id="zipProvider" runat="server"
Title="Zip Code Provider" />
<aspsample:weatherwebpart id="zipConsumer" runat="server"
Title="Zip Code Consumer" />
</zonetemplate>
</asp:webpartzone>
<asp:connectionszone id="connectionsZone1" runat="server" >
<cancelverb text="Terminate" />
<closeverb text="Close Zone" />
<configureverb text="Configure" />
<connectverb text="Connect Controls" />
<disconnectverb text="End Connection" />
</asp:connectionszone>
</div>
</form>
</body>
</html>
请在浏览器中加载该网页。使用“显示模式”下拉列表控件,将该页切换到连接模式。在“ZIP Code Provider”(邮政编码提供者)控件的谓词菜单上(由控件标题栏中的向下箭头表示),单击连接谓词。随即出现一个 ConnectionsZone 控件。注意,连接用户界面中出现了一个“结束连接”按钮;因为已在页面的标记中声明了连接,所以控件已连接。单击“结束连接”,然后再次使用“显示模式”控件使该页返回到浏览模式。接下来,使该页再次返回到连接模式,在一个控件上单击连接谓词,注意现在连接用户界面显示一个超链接,使您可以在控件之间形成连接。单击该链接,使用连接用户界面选择连接点并建立连接。
- ASP.NET - ConnectionsZone Web 服务器控件概述
- ASP.NET - Web 服务器控件概述
- ASP.NET - AdRotator Web 服务器控件概述
- ASP.NET - BulletedList Web 服务器控件概述
- ASP.NET - Button Web 服务器控件概述
- ASP.NET - HiddenField Web 服务器控件概述
- ASP.NET - HyperLink Web 服务器控件概述
- ASP.NET - Image Web 服务器控件概述
- ASP.NET - Label Web 服务器控件概述
- ASP.NET - Literal Web 服务器控件概述
- ASP.NET - 本地化 Web 服务器控件概述
- ASP.NET - Panel Web 服务器控件概述
- ASP.NET - PlaceHolder Web 服务器控件概述
- ASP.NET - Substitution Web 服务器控件概述
- ASP.NET - TextBox Web 服务器控件概述
- ASP.NET - XML Web 服务器控件概述
- ASP.NET - LinqDataSource Web 服务器控件概述
- ASP.NET - SqlDataSource Web 服务器控件概述
- 初识加密算法
- 关于javascript控制Windows Media Player
- inux下安装新硬盘并加载使用手记
- 很多日本鬼子
- Linux netfilter源码分析(1)
- ASP.NET - ConnectionsZone Web 服务器控件概述
- [HL7_V2.6]查询医嘱XML
- UE,EP添加右键
- oracle在linux下启动相关命令
- [学习笔记]寄存器使用注意事项
- 理解javascript arguments的例子
- ASP.NET - DeclarativeCatalogPart Web 服务器控件概述
- JavaScript继承详解
- Linux netfilter源码分析(2)