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

80酷酷网    80kuku.com

  asp.net|web|程序|创建

1.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>2.ConnectionsZone 控件
提供供Web Part进行通讯的UI,最终用户,而不是开发人员创建通讯关系
<asp:ConnectionsZone ID="ConnectionsZone1"
  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"
[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>4 定制Web Parts
增加自定义操作
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) {  }
 
}5.导出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
    {  }
 
}

 

分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: