简单说一下这个类,它是从Microsoft.SharePoint.WebPartPages.WebPart继承而来,并且重载了CreateChildControls和RenderContents方法,以此来载入和显示我们在前面创建的ASCX Web控件。这个类继承了Microsoft.SharePoint.WebPartPages.WebPart类,所以你需要根据你的名称空间或类段来添加正确的using代码。
using Microsoft.SharePoint.WebPartPages;设置这个类和WebPart的继承关系:
public class WebParticle: WebPart我定义了两个属性,它们一起决定了载入Web Control ASCX文件的位置。如果你希望简化这个类的继承,只需重写这些属性的声明,它们一个定义了源目录,另一个定义了含有你的控件的文件名。
protected string UserControlPath = @"~/usercontrols/";接下来,类会重载CreateChildControls方法来载入Web控件。在这个方法中,控件被从System.Web.UI.Control继承的Page属性通过Microsoft.SharePoint.WebPartPages.WebPart从源文件载入。Page属性允许到下面的ASP.NET Page例子(包括SharePoint的WebPart)的可编程接入。
protected string UserControlFileName = @"webparticlecontrol.ascx";
Collapse
protected override void CreateChildControls()
{
try
{
// load the control ... this could require GAC installation
// of your DLL to avoid File.IO permissions denial exceptions
_control = this.Page.LoadControl(UserControlPath + UserControlFileName);
// add it to the controls collection to wire up events
Controls.Add(_control);
}
catch (Exception CreateChildControls_Exception)
{
_exceptions += "CreateChildControls_Exception: " + CreateChildControls_Exception.Message;
if (AlwaysBubbleUpExceptions)
{
throw;
}
}//end catch
finally
{
base.CreateChildControls();
}//end try/catch/finally block
}//end protected override void CreateChildControls()