现在的位置: 首页 > 综合 > 正文

让用户控件包装器“见鬼”去。

2012年02月16日 ⁄ 综合 ⁄ 共 1625字 ⁄ 字号 评论关闭

   把用户控件装载到到WebPart里面,在实际的项目中,我们有时候需要用很多用户控件在MOSS里面,如果用,用户控件包装器的话,客户一看就显示的很不专业,也影响公司的形象,所以,做项目的时候需要把用户控件包装成WebPart,这样就显示的稍微的好点咯。。。嘿嘿。
其实也很简单,
1。 步骤1写好自己的用户控件,然后,把他用户控件的页面放到
C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\CONTROLTEMPLATES\WpLoadUc\AddUserControl.ascx下面
然后把dll放到相应的moss bin 下面 或GAC
2。写一个WebPart,很简单,代码如下:

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
{
    [Guid("0d2c817f-71c3-4349-b813-cf8eb81f4bd5")]
    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 = @"/_controltemplates/WpLoadUc/AddUserControl.ascx";
            this.userControl = this.Page.LoadControl(userControlPath);
            this.Controls.Add(this.userControl);

        }
        protected override void Render(HtmlTextWriter writer)
        {
            // TODO: add custom rendering code here.
             writer.Write("Show AA");
             this.userControl.RenderControl(writer);

        }

    }
}
代码是不是很简单哦,WebPart 的部署就不用我说啦。。。
3,然后修改下配置文件
<SafeControl Src="~/_controltemplates/WpLoadUc/*" IncludeSubFolders="True" Safe="True" AllowRemoteDesigner="True" />

嘿嘿 指定到相应的用户控件哦。
哈哈哈 ,各位是不是很简单,,嘿嘿快去测试哦。/
。。。。。。。。。。。。。
代码不是最好的,只是供大家参考。。

抱歉!评论已关闭.