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
    {  }
  
}
新聞熱點
疑難解答
圖片精選