從開發webpart的過程,我們也可以看出,假若想開發功能比較複雜的webpart,也就是表現樣式比較豐富的webpart,用我們上面的辦法是行不通的。于是,我們就要想想有沒有别的辦法了。其實,我們仔細考慮下wss3中webpart從哪個類繼承就知道該怎麼作了。由于,無論是wss3還是asp.net2中的webpart都是從
System.Web.UI.WebControls.WebParts.WebPart繼承的,而webpart又是從
System.Web.UI.Control繼承的。于是,我們就可以考慮用Page.LoadControl(filepath)方法載入使用者控件了。而此種方法實作的webpart就叫使用者控件包裝器。包裝器的實作與使用者自定義控件的開發原理也就一樣了。也就是要重寫CreateChildControls ()方法和Render (HtmlTextWriter writer)方法。我們可以在CreateChildControls ()方法中動手腳,也就是在這個控件裡載入我們的自定義控件就行了。
使用者控件包裝器,現在常用的是quickstart,對其詳細使用介紹大家可以參考下面這篇文章http://www.cnblogs.com/cxd4321/archive/2007/08/28/873326.html。
包裝器實作例子如下:
using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
namespace WPLoadUserControl
{
public class WPLoadUserControl
: System.Web.UI.WebControls.WebParts.WebPart
{
protected Control userControl;
public WPLoadUserControl()
{
this.ExportMode = WebPartExportMode.All;
}
protected override void CreateChildControls()
//base.CreateChildControls();
this.Controls.Clear();
string userControlPath = @"/Skins/TestControl.ascx";
this.userControl = this.Page.LoadControl(userControlPath);
this.Controls.Add(this.userControl);
protected override void Render(HtmlTextWriter writer)
base.Render(writer);
}