自己动手编写一个类似 QuickPart 的用户控件包装器
2009-01-19 17:58 闫妍 阅读(125) 评论(0) 编辑 收藏 举报大家知道涂曙光写了一个QuickPart的包装器,那么大家是否想自己来写一个这样的包装器呢,别担心其实并不难,自己去写一个包装器的好处是,你可以定制这个包装器样式和属性等,大家只要复制我下面的代码进行编译就可以了。
首先是建立WebPart项目,你可以选择建立一个类库,然后引用相应的程序集,建立一个WebPart程序集,你也可以直接建立一个WebPart工程,我们下面使用后面这种方法,只是为了简化部署的过程,对于部署网上的资料真是很多,同学们可以找一下。
1.建立一个WebPart工程
2.建立一个删除WebPart1文件夹,建立一个UCWebPart 的WebPart
3.复制下面的代码到你的UCWebPart.Cs 文件中,建立Zone
Code
Code
using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;
using System.ComponentModel;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
using System.Collections;
namespace UltraWebPart
{
[Guid("e58fdeb2-2fe5-4119-9b47-d80818adc876")]
public class UCWebPart : System.Web.UI.WebControls.WebParts.WebPart, IWebEditable
{
private UserControl usercontrol;
public UCWebPart()
{
}
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;
}
}
public EditorPartCollection CreateEditorParts()
{
UltraWebPart.UCWebPartEditor obj = new UltraWebPart.UCWebPartEditor();
obj.ID = this.ID + "_WebPartEditor";
obj.Title = "选择附加功能";
ArrayList EditorParts = new ArrayList();
EditorParts.Add(obj);
return new EditorPartCollection(EditorParts);
}
}
}
Code
using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;
using System.ComponentModel;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
using System.Collections;
namespace UltraWebPart
{
[Guid("e58fdeb2-2fe5-4119-9b47-d80818adc876")]
public class UCWebPart : System.Web.UI.WebControls.WebParts.WebPart, IWebEditable
{
private UserControl usercontrol;
public UCWebPart()
{
}
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;
}
}
public EditorPartCollection CreateEditorParts()
{
UltraWebPart.UCWebPartEditor obj = new UltraWebPart.UCWebPartEditor();
obj.ID = this.ID + "_WebPartEditor";
obj.Title = "选择附加功能";
ArrayList EditorParts = new ArrayList();
EditorParts.Add(obj);
return new EditorPartCollection(EditorParts);
}
}
}
4.再建立一个类库文件,UCWebPartEditor.cs 将下面的代码复制到其中,建立一个Editor
Code
Code
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.SharePoint;
using System.IO;
namespace UltraWebPart
{
public class UCWebPartEditor : System.Web.UI.WebControls.WebParts.EditorPart
{
private DropDownList ddlTree = new DropDownList();
public override bool ApplyChanges()
{
UltraWebPart.UCWebPart part = (UltraWebPart.UCWebPart)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(HtmlTextWriter writer)
{
base.RenderChildren(writer);
}
public override void RenderControl(HtmlTextWriter writer)
{
base.RenderControl(writer);
}
}
}
Code
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.SharePoint;
using System.IO;
namespace UltraWebPart
{
public class UCWebPartEditor : System.Web.UI.WebControls.WebParts.EditorPart
{
private DropDownList ddlTree = new DropDownList();
public override bool ApplyChanges()
{
UltraWebPart.UCWebPart part = (UltraWebPart.UCWebPart)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(HtmlTextWriter writer)
{
base.RenderChildren(writer);
}
public override void RenderControl(HtmlTextWriter writer)
{
base.RenderControl(writer);
}
}
}
5. 编译并部署这个项目到SharePoint 站点
6. 打开WSS站点,添加这个WebPart,像使用QuickPart一样的去使用,怎么样是不是很简单呢。