65.9K
CodeProject 正在变化。 阅读更多。
Home

简单的 WebPart 加载 UserControl

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.60/5 (4投票s)

2008年9月12日

CPOL

1分钟阅读

viewsIcon

86075

downloadIcon

819

这是一个简单的 WebPart,但对于加载许多 UserControl 非常有用

引言

Moss/WSS 目前是一款非常流行的 CMS。在这个产品上可以完成很多事情,并且可以进行深入的技术修改。

使用该解决方案时,经常会用到 WebPart。但如果我们开发一个 WebPart,有时会重复做同样的事情,即编写 WebPart 并加载我们想要的用户控件。

背景

这个概念类似于 SmartPart,你只需要创建一个 WebPart,然后就可以用来加载所有用户控件,而无需再次编写 WebPart。

Using the Code

代码如下所示

  1. 在你的类解决方案中这样编写。请同时引用 Microsoft.Sharepoint.dll 和 System.Web.dll 组件。
using System;
using System.ComponentModel;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.WebPartPages;

namespace WebPartLib
{
    public class WebPartGeneral : Microsoft.SharePoint.WebPartPages.WebPart
    {
        private Control _childControl = null;
        private string _userControlVirtualPath = string.Empty;
        private string _errMessage = string.Empty;

        public WebPartGeneral()
        { }

        [
          Personalizable(),
          Category("Miscellaneous"),
          DefaultValue(""),
          WebBrowsable(true),
          WebDisplayName("User Control Virtual Path"),
          WebDescription("User Control Virtual Path")
        ]
        public string UserControlVirtualPath
        {
            get { return _userControlVirtualPath; }
            set { _userControlVirtualPath = value; }
        }

        protected override void RenderWebPart(HtmlTextWriter output)
        {
            if (_errMessage != string.Empty) output.Write(_errMessage);
            if (_userControlVirtualPath != string.Empty ||
                _userControlVirtualPath.Equals("") == false)
               RenderChildren(output);
        }

        protected override void RenderChildren(HtmlTextWriter output)
        {
            try
            {
                this.EnsureChildControls();
                if (this._childControl != null)
                    this._childControl.RenderControl(output);
            }
            catch (Exception ex)
            {
                _errMessage = string.Format(
                  "Exception Message (RenderWebPart) = {0}<br />", ex.Message);
            }
        }

        protected override void CreateChildControls()
        {
            base.CreateChildControls();
            if (_userControlVirtualPath != string.Empty)
            {
                if (_childControl != null) return;
                _childControl = Page.LoadControl(_userControlVirtualPath);
                if (_childControl != null)
                    Controls.AddAt(0, _childControl);
            }
        }

        public override void Dispose()
        {
             
        }
    }
}
  1. 解决方案编译成功后,将二进制文件放入 MOss/Wss 站点的 bin 文件夹中。之后,再创建一个名为 UserControls 的文件夹。

  1. 修改你的 web.config
eControl Assembly="WebPartLib, Version=1.0.0.0, Culture=neutral"
   Namespace="WebPartLib" TypeName="*" Safe="True"
   AllowRemoteDesigner="True" /> 

并将信任级别从 WSS_Minimal 更改为 Full。

<trust level="Full" originUrl="" /> 
  1. 如果需要,你可以重启 IIS,然后返回你的站点,将 WebPart 列入 WebPart 列表。

  2. 创建一个新的网站解决方案,创建一个简单的用户控件,然后将所有 .ascx 和 .cs 文件复制到 UserControls 文件夹中。
  3. 现在你可以使用 WebPart,并将虚拟路径设置为你的用户控件,然后,神奇般地,你的 WebPart 就会加载你的用户控件。你可以创建另一个用户控件,然后再次添加你的 WebPart 并设置要加载的用户控件。

关注点

正如我之前提到的,现在每次创建用户控件,你都不需要创建另一个 WebPart。只需再次使用该 WebPart,并将路径设置为该用户控件即可。

© . All rights reserved.