且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

ASP.NET 2.0使用Web Part创建应用程序之二(共二)

更新时间:2022-04-23 02:15:57


1.Web Part 通讯
Web Parts可以相互通讯,提供者发布接口,订阅者通过接口获得数据,WebPartManager 管理通讯,从提供者获得接口,向订阅者发布接口,通讯可以是静态的,也可以是动态的,ConnectionsZone 提供后期绑定的UI
通讯提供者
实现方法返回接口,方法特性 [ConnectionProvider]
ASP.NET 2.0使用Web Part创建应用程序之二(共二)[ConnectionProvider ("Zip Code""ZipCodeProvider")]
ASP.NET 2.0使用Web Part创建应用程序之二(共二)
public IZipCode GetZipCodeInterface ()
ASP.NET 2.0使用Web Part创建应用程序之二(共二)
{
ASP.NET 2.0使用Web Part创建应用程序之二(共二)    
return this// Assumes control implements IZipCode
ASP.NET 2.0使用Web Part创建应用程序之二(共二)
}

ASP.NET 2.0使用Web Part创建应用程序之二(共二)
ASP.NET 2.0使用Web Part创建应用程序之二(共二)
// IZipCode.GetZipCode implementation
ASP.NET 2.0使用Web Part创建应用程序之二(共二)
public string GetZipCode ()
ASP.NET 2.0使用Web Part创建应用程序之二(共二)
{
ASP.NET 2.0使用Web Part创建应用程序之二(共二)    
return _zip;
ASP.NET 2.0使用Web Part创建应用程序之二(共二)}
通讯订阅者
实现方法接收接口参数,方法特性 [ConnectionConsumer]
ASP.NET 2.0使用Web Part创建应用程序之二(共二)[ConnectionConsumer ("Zip Code""ZipCodeConsumer")]
ASP.NET 2.0使用Web Part创建应用程序之二(共二)
public void GetIZipCodeInterface (IZipCode provider)
ASP.NET 2.0使用Web Part创建应用程序之二(共二)
{
ASP.NET 2.0使用Web Part创建应用程序之二(共二)    
string zip = provider.GetZipCode (); // Get zip code from provider
ASP.NET 2.0使用Web Part创建应用程序之二(共二)
      ASP.NET 2.0使用Web Part创建应用程序之二(共二)
ASP.NET 2.0使用Web Part创建应用程序之二(共二)}
静态通讯方式
在 WebPartManager的 <StaticConnections> 元素中定义,最终用户无法修改
ASP.NET 2.0使用Web Part创建应用程序之二(共二)<asp:Connection>的实例
ASP.NET 2.0使用Web Part创建应用程序之二(共二)
<asp:WebPartManager ID="WebPartManager1" Runat="server">
ASP.NET 2.0使用Web Part创建应用程序之二(共二)  
<StaticConnections>
ASP.NET 2.0使用Web Part创建应用程序之二(共二)    
<asp:Connection ID="ZipCodeConnection" Runat="server"
ASP.NET 2.0使用Web Part创建应用程序之二(共二)      ProviderID
="Weather1" ProviderConnectionPointID="ZipCodeProvider"
ASP.NET 2.0使用Web Part创建应用程序之二(共二)      ConsumerID
="News1" ConsumerConnectionPointID="ZipCodeConsumer" />
ASP.NET 2.0使用Web Part创建应用程序之二(共二)  
</StaticConnections>
ASP.NET 2.0使用Web Part创建应用程序之二(共二)
</asp:WebPartManager>
2.ConnectionsZone 控件
提供供Web Part进行通讯的UI,最终用户,而不是开发人员创建通讯关系
ASP.NET 2.0使用Web Part创建应用程序之二(共二)<asp:ConnectionsZone ID="ConnectionsZone1"
ASP.NET 2.0使用Web Part创建应用程序之二(共二)  Runat
="server" />
3.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"
ASP.NET 2.0使用Web Part创建应用程序之二(共二)[WebBrowsable]
ASP.NET 2.0使用Web Part创建应用程序之二(共二)[Personalizable]
ASP.NET 2.0使用Web Part创建应用程序之二(共二)
public string Stocks
ASP.NET 2.0使用Web Part创建应用程序之二(共二)
{
ASP.NET 2.0使用Web Part创建应用程序之二(共二)    
get return _stocks; }
ASP.NET 2.0使用Web Part创建应用程序之二(共二)    
set { _stocks =  value; }
ASP.NET 2.0使用Web Part创建应用程序之二(共二)}

ASP.NET 2.0使用Web Part创建应用程序之二(共二)
ASP.NET 2.0使用Web Part创建应用程序之二(共二)Shared Personalization
ASP.NET 2.0使用Web Part创建应用程序之二(共二)[Personalizable (PersonalizationScope.
-Shared)] persists properties on shared basis
ASP.NET 2.0使用Web Part创建应用程序之二(共二)
string _stocks; // e.g., "MSFT,INTC,AMZN"
ASP.NET 2.0使用Web Part创建应用程序之二(共二)

ASP.NET 2.0使用Web Part创建应用程序之二(共二)[WebBrowsable]
ASP.NET 2.0使用Web Part创建应用程序之二(共二)[Personalizable (PersonalizationScope.Shared)]
ASP.NET 2.0使用Web Part创建应用程序之二(共二)
public string Stocks
ASP.NET 2.0使用Web Part创建应用程序之二(共二)
{
ASP.NET 2.0使用Web Part创建应用程序之二(共二)    
get return _stocks; }
ASP.NET 2.0使用Web Part创建应用程序之二(共二)    
set { _stocks =  value; }
ASP.NET 2.0使用Web Part创建应用程序之二(共二)}

ASP.NET 2.0使用Web Part创建应用程序之二(共二)
个性化服务是基于Provider模式
ASP.NET 2.0使用Web Part创建应用程序之二(共二)使用 SQL Server Provider
ASP.NET 2.0使用Web Part创建应用程序之二(共二)
<configuration>
ASP.NET 2.0使用Web Part创建应用程序之二(共二)  
<system.web>
ASP.NET 2.0使用Web Part创建应用程序之二(共二)    
<webParts>
ASP.NET 2.0使用Web Part创建应用程序之二(共二)      
<personalization defaultProvider="AspNetSqlPersonalizationProvider" />
ASP.NET 2.0使用Web Part创建应用程序之二(共二)    
</webParts>
ASP.NET 2.0使用Web Part创建应用程序之二(共二)  
</system.web>
ASP.NET 2.0使用Web Part创建应用程序之二(共二)
</configuration>
4 定制Web Parts
增加自定义操作
ASP.NET 2.0使用Web Part创建应用程序之二(共二)public class MyWebPart : WebPart
ASP.NET 2.0使用Web Part创建应用程序之二(共二)
{
ASP.NET 2.0使用Web Part创建应用程序之二(共二)    
public override WebPartVerbCollection Verbs
ASP.NET 2.0使用Web Part创建应用程序之二(共二)    
{
ASP.NET 2.0使用Web Part创建应用程序之二(共二)        
get {
ASP.NET 2.0使用Web Part创建应用程序之二(共二)            EnsureChildControls ();
ASP.NET 2.0使用Web Part创建应用程序之二(共二)            WebPartVerb verb 
=
ASP.NET 2.0使用Web Part创建应用程序之二(共二)                
new WebPartVerb (new WebPartEventHandler (OnClearResults));
ASP.NET 2.0使用Web Part创建应用程序之二(共二)            verb.Text 
= "Clear Results";
ASP.NET 2.0使用Web Part创建应用程序之二(共二)            WebPartVerb[] verbs 
= new WebPartVerb[] { verb };
ASP.NET 2.0使用Web Part创建应用程序之二(共二)            
return new WebPartVerbCollection (base.Verbs, verbs);
ASP.NET 2.0使用Web Part创建应用程序之二(共二)        }

ASP.NET 2.0使用Web Part创建应用程序之二(共二)    }

ASP.NET 2.0使用Web Part创建应用程序之二(共二)
ASP.NET 2.0使用Web Part创建应用程序之二(共二)    
void OnClearResults (object sender, WebPartEventArgs args) ASP.NET 2.0使用Web Part创建应用程序之二(共二) }
ASP.NET 2.0使用Web Part创建应用程序之二(共二)  ASP.NET 2.0使用Web Part创建应用程序之二(共二)
ASP.NET 2.0使用Web Part创建应用程序之二(共二)}
5.导出Web Part
WebPart.ExportMode属性,WebPartExportMode.None (默认),WebPartExportMode.All
WebPartExportMode.NonSensitiveData,All” 及 “NonSensitiveData” 增加导出操作以便Web Part可以被导出
仅[Personalizable] 属性,PersonalizableAttribute.IsSensitive识别 “sensitive” 属性
导出所有属性
ASP.NET 2.0使用Web Part创建应用程序之二(共二)public class MyWebPart : WebPart
ASP.NET 2.0使用Web Part创建应用程序之二(共二)
{
ASP.NET 2.0使用Web Part创建应用程序之二(共二)    
public MyWebPart ()
ASP.NET 2.0使用Web Part创建应用程序之二(共二)    
{
ASP.NET 2.0使用Web Part创建应用程序之二(共二)        ExportMode 
= WebPartExportMode.All;
ASP.NET 2.0使用Web Part创建应用程序之二(共二)    }

ASP.NET 2.0使用Web Part创建应用程序之二(共二)  ASP.NET 2.0使用Web Part创建应用程序之二(共二)
ASP.NET 2.0使用Web Part创建应用程序之二(共二)}
导出所选择的属性
ASP.NET 2.0使用Web Part创建应用程序之二(共二)public class MyWebPart : WebPart
ASP.NET 2.0使用Web Part创建应用程序之二(共二)
{
ASP.NET 2.0使用Web Part创建应用程序之二(共二)    
public MyWebPart ()
ASP.NET 2.0使用Web Part创建应用程序之二(共二)    
{
ASP.NET 2.0使用Web Part创建应用程序之二(共二)        ExportMode 
= WebPartExportMode.NonSensitiveData;
ASP.NET 2.0使用Web Part创建应用程序之二(共二)    }

ASP.NET 2.0使用Web Part创建应用程序之二(共二)
ASP.NET 2.0使用Web Part创建应用程序之二(共二)    
// This property will be exported
ASP.NET 2.0使用Web Part创建应用程序之二(共二)
    [Personalizable (PersonalizationScope.User, false)]
ASP.NET 2.0使用Web Part创建应用程序之二(共二)    
public string ZipCode
ASP.NET 2.0使用Web Part创建应用程序之二(共二)    
ASP.NET 2.0使用Web Part创建应用程序之二(共二) }
ASP.NET 2.0使用Web Part创建应用程序之二(共二)
ASP.NET 2.0使用Web Part创建应用程序之二(共二)    
// This one will not
ASP.NET 2.0使用Web Part创建应用程序之二(共二)
    [Personalizable (PersonalizationScope.User, true)]
ASP.NET 2.0使用Web Part创建应用程序之二(共二)    
public string SocialSecurityNumber
ASP.NET 2.0使用Web Part创建应用程序之二(共二)    
ASP.NET 2.0使用Web Part创建应用程序之二(共二) }
ASP.NET 2.0使用Web Part创建应用程序之二(共二)  ASP.NET 2.0使用Web Part创建应用程序之二(共二)
ASP.NET 2.0使用Web Part创建应用程序之二(共二)}



本文转自高海东博客园博客,原文链接:http://www.cnblogs.com/ghd258/archive/2005/11/22/282367.html,如需转载请自行联系原作者