ASP.NET2.0 WebPart实例教程

WebPartManager 控件
Web Parts的总控中心
管理 Web Parts及区域的列表
管理页面状态 (比如显示状态),当页面状态时发生改变时触发事件
协助Web Parts间的通讯
管理个性化等
<asp:WebPartManager ID="WebPartManager1" RunAt="server" />

WebPartZone 控件
在 Web Parts页面中定义区域
定义每个区域当中Web Part的默认显示样式及布局
<asp:WebPartZone ID="WeatherZone"
DragHighlightColor="244,198,96" RunAt="server">
<PartTitleStyle BackColor="#2254B1" ForeColor="White" />
<PartStyle BorderColor="#81AAF2" BorderStyle="Solid" BorderWidth="1px" />
<ZoneTemplate>
<!-- Web Parts declared here -->
</ZoneTemplate>
</asp:WebPartZone>

Web Part Chrome
标题框及Web Part的边框
通过 WebPartZone 定义其外观

Web Parts
在 WebPartZone中定义的控件
Web controls, user controls, custom controls
未实现IWebPart接口的控件将封装进GenericWebParts
增加以下属性: Title, Description, etc.
<ZoneTemplate>
<asp:Calendar Title="Calendar" ID="Calendar1" RunAt="server" />
<user:Weather Title="Weather" ID="Weather1" RunAt="server" />
<custom:Search Title="Search" ID="Search1" RunAt="server" />
</ZoneTemplate>

WebPartManager.DisplayMode
设置或者获取页面的显示模式
BrowserDisplayMode “正常的” 显示模式,无法编辑(默认)
DesignDisplayMode 允许拖拽式布局编辑
EditDisplayMode 允许编辑Web Part的外观及行为
CatalogDisplayMode 允许将Web Part添加在另外的页面上
ConnectDisplayMode 允许Web Parts之间进行通讯

DisplayMode 事件
WebPartManager.DisplayModeChanging
指示显示模式是否要发生改变
WebPartDisplayModeCancelEventArgs 获得新的显示模式并且允许控制者取消这种改变
WebPartManager.DisplayModeChanged
指示显示模式是否已经发生改变
WebPartDisplayModeEventArgs 得到原来的显示模式

其它的区域类型
WebPartZones 定义基本的页面显示内容
在任何时间均显示
其它区域允许页面可以被编辑
根据显示模式有条件得进行显示
CatalogZone 允许用户在页面上添加Web Part
EditorZone 允许用户更改Web Part的属性及布局
ConnectionsZone 允许用户建立Web Part之间的通讯

CatalogZone 控件
允许Web Part可以交互式的进行添加
包含一个或者多个 CatalogPart 控件
PageCatalogPart 显示页面上已经删除的Web Part的列表
DeclarativeCatalogPart 显示声明在 <WebPartsTemplate>中的Web Part的列表
ImportCatalogPart 允许从 .WebPart文件中导入的Web Part

声明 CatalogZone
<asp:CatalogZone ID="CatalogZone1" Runat="server">
<ZoneTemplate>
<asp:PageCatalogPart ID="PageCatalogPart1" Runat="server" />
<asp:DeclarativeCatalogPart ID="DeclarativeCatalogPart1" Runat="server">
<WebPartsTemplate>
<!-- Declarative Web Parts go here -->
</WebPartsTemplate>
</asp:DeclarativeCatalogPart>
<asp:ImportCatalogPart ID="ImportCatalogPart1" Runat="server" />
</ZoneTemplate>
</asp:CatalogZone>

EditorZone控件
允许交互式的对 Web parts进行更改
包含一个或者多个 EditorPart 控件
AppearanceEditorPart 提供修改标题及其它界面相关属性的UI
BehaviorEditorPart 提供修改行为属性的UI
LayoutEditorPart 提供修改Web Part的显示状态,区域及区域索引的UI
PropertyGridEditorPart 提供修改定制属性的UI
声明 EditorZone
<asp:EditorZone ID="EditorZone1" Runat="server">
<ZoneTemplate>
<asp:AppearanceEditorPart ID="AppearanceEditorPart1" Runat="server" />
<asp:BehaviorEditorPart ID="BehaviorEditorPart1" Runat="server" />
<asp:LayoutEditorPart ID="LayoutEditorPart1" Runat="server" />
</ZoneTemplate>
</asp:EditorZone>

PropertyGridEditorPart
允许修改自定义属性的UI
显示标记为 [WebBrowsable]的属性
string _stocks; // e.g., "MSFT,INTC,AMZN"

[WebBrowsable]
public string Stocks
{
get { return _stocks; }
set { _stocks = value; }
}

Web Part 通讯
Web Parts可以相互通讯
提供者发布接口
订阅者通过接口获得数据
WebPartManager 管理通讯
从提供者获得接口
向订阅者发布接口
通讯可以是静态的,也可以是动态的
ConnectionsZone 提供后期绑定的UI

通讯提供者
实现方法返回接口
方法特性 [ConnectionProvider]
[ConnectionProvider ("Zip Code", "ZipCodeProvider")]
public IZipCode GetZipCodeInterface ()
{
return this; // Assumes control implements IZipCode
}

// IZipCode.GetZipCode implementation
public string GetZipCode ()
{
return _zip;
}
通讯订阅者
实现方法接收接口参数
方法特性 [ConnectionConsumer]
[ConnectionConsumer ("Zip Code", "ZipCodeConsumer")]
public void GetIZipCodeInterface (IZipCode provider)
{
string zip = provider.GetZipCode (); // Get zip code from provider
...
}
静态通讯方式
在 WebPartManager的 <StaticConnections> 元素中定义
最终用户无法修改
<asp:Connection>的实例
<asp:WebPartManager ID="WebPartManager1" Runat="server">
<StaticConnections>
<asp:Connection ID="ZipCodeConnection" Runat="server"
ProviderID="Weather1" ProviderConnectionPointID="ZipCodeProvider"
ConsumerID="News1" ConsumerConnectionPointID="ZipCodeConsumer" />
</StaticConnections>
</asp:WebPartManager>

ConnectionsZone 控件
提供供Web Part进行通讯的UI
最终用户,而不是开发人员创建通讯关系
<asp:ConnectionsZone ID="ConnectionsZone1"
Runat="server" />

Web Parts 个性化
Web Parts 个性化服务
自动保存相关Web Part的属性 (布局, 外观等等)
自动保存标记为 PersonalizableAttribute的定制属性
PersonalizationAdministration 类
提供个性化服务的API
Provider-based for flexible data storage
Per-User 个性化
[Personalizable] 为每位用户保存定制属性
string _stocks; // e.g., "MSFT,INTC,AMZN"

[WebBrowsable]
[Personalizable]
public string Stocks
{
get { return _stocks; }
set { _stocks = value; }
}

Shared Personalization
[Personalizable (PersonalizationScope.-Shared)] persists properties on shared basis
string _stocks; // e.g., "MSFT,INTC,AMZN"

[WebBrowsable]
[Personalizable (PersonalizationScope.Shared)]
public string Stocks
{
get { return _stocks; }
set { _stocks = value; }
}
个性化服务是基于Provider模式
使用 SQL Server Provider
<configuration>
<system.web>
<webParts>
<personalization defaultProvider="AspNetSqlPersonalizationProvider" />
</webParts>
</system.web>
</configuration>

定制Web Parts
任何控件均可以作为Web Parts来运行,但…
Controls that derive from WebPart can better leverage the Web Parts infrastructure
控件标题及其它与UI相关的属性
控件的AllowClose, AllowZoneChange, AllowMinimize, 以及其它行为属性
应用基于角色的安全特性 (授权方式)
增加自定义操作,导出Web Part以及更多..
增加自定义操作
public class MyWebPart : WebPart
{
public override WebPartVerbCollection Verbs
{
get {
EnsureChildControls ();
WebPartVerb verb =
new WebPartVerb (new WebPartEventHandler (OnClearResults));
verb.Text = "Clear Results";
WebPartVerb[] verbs = new WebPartVerb[] { verb };
return new WebPartVerbCollection (base.Verbs, verbs);
}
}

void OnClearResults (object sender, WebPartEventArgs args) { ... }
...
}
导出Web Part
WebPart.ExportMode属性
WebPartExportMode.None (默认)
WebPartExportMode.All
WebPartExportMode.NonSensitiveData
“All” 及 “NonSensitiveData” 增加导出操作以便Web Part可以被导出
仅[Personalizable] 属性
PersonalizableAttribute.IsSensitive识别 “sensitive” 属性
导出所有属性
public class MyWebPart : WebPart
{
public MyWebPart ()
{
ExportMode = WebPartExportMode.All;
}
...
}
导出所选择的属性
public class MyWebPart : WebPart
{
public MyWebPart ()
{
ExportMode = WebPartExportMode.NonSensitiveData;
}

// This property will be exported
[Personalizable (PersonalizationScope.User, false)]
public string ZipCode
{ ... }

// This one will not
[Personalizable (PersonalizationScope.User, true)]
public string SocialSecurityNumber
{ ... }
...
}

原文地址:https://www.cnblogs.com/jackzhang/p/610208.html