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
{ }
}