zoukankan      html  css  js  c++  java
  • 如何自己写一个用户控件包装器来代替QuickPart! cloud

    如何自己写一个用户控件包装器来代替QuickPart!

    具体步骤如下:

    1.打开VS2008,新建一个Web Part类型的项目工程,命名为SuperWebPart

    注:Windows SharePoint Services 3.0 工具:Visual Studio 2008 Extensions 1.2 版,下载地址如下:

    http://www.microsoft.com/downloads/details.aspx?familyid=7BF65B28-06E2-4E87-9BAD-086E32185E68&displaylang=zh-cn  (中文版)

    http://www.microsoft.com/downloads/details.aspx?displaylang=en&FamilyID=7bf65b28-06e2-4e87-9bad-086e32185e68    (英文版)

    新建工程如下图:

    2.点击OK后,在项目中新建一个名称为 TreeWebPart.cs 类库文件和一个类型为Web Part的文件(命名为SuperWebPart),如下图:

     

    3.在TreeWebPart.cs类库文件中编写代码,此类继承System.Web.UI.WebControls.WebParts.EditorPart这个编辑部件,目的用于编辑System.Web.UI.WebControls.WebParts.WebPart控件,并且需要重写ApplyChanges,SyncChanges,CreateChildControls等方法,

    完整代码如下

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Data;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using Microsoft.SharePoint;
    using System.IO;
    using System.Configuration;

    namespace SuperWebPart
    {
        
    class TreeWebPart:EditorPart
        {
            
    private DropDownList ddlTree = new DropDownList();

            
    public override bool ApplyChanges()
            {
                SuperWebPart part 
    = (SuperWebPart)WebPartToEdit;
                part.Url 
    = this.ddlTree.SelectedItem.Value;
                
    return true;
            }

            
    public override void SyncChanges()
            {

            }

            
    protected override void CreateChildControls()
            {
                
    base.CreateChildControls();

                
    //从wpresources目录下找出所有的文件
                string[] a = System.IO.Directory.GetFiles(HttpContext.Current.Server.MapPath("../wpresources"));
                FileInfo info;
                
    for (int i = 0; i < a.Length; i++)
                {
                    info 
    = new FileInfo(a[i]);
                    ddlTree.Items.Add(info.Name);
                }

                Controls.Add(ddlTree);
            }

            
    protected override void RenderChildren(System.Web.UI.HtmlTextWriter writer)
            {
                
    base.RenderChildren(writer);
            }

            
    public override void RenderControl(System.Web.UI.HtmlTextWriter writer)
            {
                
    base.RenderControl(writer);
            }
        }
    }

    4.SuperWebPart.cs类库文件的完整代码如下,此类需要从WebParts和IWebEditable接口继承:

    using System;
    using System.Runtime.InteropServices;
    using System.Web.UI;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Serialization;
    using System.Collections;
    using System.ComponentModel;

    using Microsoft.SharePoint;
    using Microsoft.SharePoint.WebControls;
    using Microsoft.SharePoint.WebPartPages;

    namespace SuperWebPart
    {
        [Guid(
    "d833c329-1066-4c59-a73b-38341ae07797")]
        
    public class SuperWebPart : System.Web.UI.WebControls.WebParts.WebPart,IWebEditable
        {
            
    public SuperWebPart()
            {
            }

            
    private UserControl usercontrol;

            
    protected override void CreateChildControls()
            {
                
    base.CreateChildControls();
                Controls.Clear();

                
    if (this.Url != string.Empty)
                {
                    usercontrol 
    = (UserControl)Page.LoadControl(@"/wpresources/" + this.Url);
                    Controls.Add(usercontrol);
                }
            }

            
    protected override void Render(HtmlTextWriter writer)
            {
                EnsureChildControls();
                
    if (usercontrol != null)
                {
                    usercontrol.RenderControl(writer);
                }
            }

            
    private string _url = string.Empty;

            [Personalizable]
            [WebBrowsable]
            [WebDisplayName(
    "请手工输入一个控件文件")]
            [WebDescription(
    "只需要文件名就行了")]
            [Category(
    "个性设置")]
            
    public string Url
            {
                
    get { return _url; }
                
    set { _url = value; }
            }

            
    //把TreeWebPart部件加入到俄EditorParts中
            public EditorPartCollection CreateEditorParts()
            {
                TreeWebPart obj 
    = new TreeWebPart();
                obj.ID 
    = this.ID + "_selectTreeWebpart";
                obj.Title 
    = "选择用户控件by zhangyun";
                ArrayList EditorParts 
    = new ArrayList();
                EditorParts.Add(obj);
                
    return new EditorPartCollection(EditorParts);
            }
        }
    }

    5.编译成功后,

    (1).把生成的SuperWebPart.dll放到SharePoint网站对应的bin目录下 C:\Inetpub\wwwroot\wss\VirtualDirectories\8080\bin

    ,在对应的SharePoint网站的webconfig文件中的<SafeControl></SafeControl>代码中添加如下代码:

    <SafeControl Assembly="SuperWebPart, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9f4da00116c38ec5"

    Namespace="SuperWebPart" TypeName="*" Safe="True" />,并修改 <trust level="Full" originUrl="" />

    (2).把写好用户控件(?.ascx)文件放到SharePoint网站对应的wpresources目录下 C:\Inetpub\wwwroot\wss\VirtualDirectories\8080\wpresources

    这样,SuperWebPart用户控件包装器就开发完成了,效果和QuickPart一样,最后的效果如下:

     

  • 相关阅读:
    高盛、沃尔玛 题做出来还挂了的吐槽
    amazon师兄debrief
    到所有人家距离之和最短的中点 296. Best Meeting Point
    问问题没人回答的情况怎么办终于有解了
    找名人 277. Find the Celebrity
    数组生存游戏 289. Game of Life
    547. Number of Provinces 省份数量
    428. Serialize and Deserialize Nary Tree 序列化、反序列化n叉树
    alias别名简介和使用
    面试官:线程池执行过程中遇到异常会发生什么,怎样处理? Vincent
  • 原文地址:https://www.cnblogs.com/cloudzhang/p/1417863.html
Copyright © 2011-2022 走看看