从开发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/
包装器实现例子如下:
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);
}
}
}
文章来源:http://www.cnblogs.com/cxd4321/archive/2007/08/31/877769.html