前面的文章中,我们编写了一个非常简单的WebPart,只是在网页中输出一段字符串。在实际的应用中没有什么作用。此文和大家分享如何开发高级的WebPart,为控件添加事件,实现有复合控件的WebPart等,使我们的WebPart更佳实用。
如果大家有开发服务器控件的基础,此文对你来说没有什么难度。想了解相关内容,参看我的《ASP.NET的服务器控件开发》系列。废话不多说,直接进入今天的主题。
Demo 1
第一个例子也很简单,点击一个按钮,显示当前系统时间。如何创建WebPart就不细说了,不清楚的朋友可以参看《SharePoint 2007图文开发教程(3)---实现简单的WebPart》。
直接看代码:
![](https://i-blog.csdnimg.cn/blog_migrate/0be121fa5b8988fbabbbc526af3b0fc0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/34031c708bfe702fe82d01ff5c6593aa.gif)
using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Xml.Serialization;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
namespace GetNow
{
[Guid("0708695c-869c-4e8f-a6e9-cfcf148f404c")]
public class GetNow : System.Web.UI.WebControls.WebParts.WebPart, INamingContainer
{
private string _text;
private HtmlTableCell _tablecell = new HtmlTableCell();
private Label lb = new Label();
private Button btn = new Button();
[WebBrowsable(true), Personalizable(true)]
public string Text
{
get { return _text; }
set { _text = value; }
}
public GetNow()
{
this.btn.Click += new EventHandler(btn_Click);
this.ExportMode = WebPartExportMode.All;
}
/// <summary>
/// 重写OnLoad
/// </summary>
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
if (this.Page.IsPostBack == true)
{
this.AddControls();
}
}
/// <summary>
/// 重写CreateChildControls
/// </summary>
protected override void CreateChildControls()
{
this.Controls.Add(new LiteralControl("<table>" + "/n"));
this.Controls.Add(new LiteralControl(" <tr>" + "/n"));
this.Controls.Add(this._tablecell);
this.Controls.Add(new LiteralControl(" </tr>" + "/n"));
this.Controls.Add(new LiteralControl("</table>" + "/n"));
if (this.Page.IsPostBack == false)
{
this.AddControls();
}
}
/// <summary>
/// click处理函数
/// </summary>
void btn_Click(object sender, EventArgs e)
{
this._text = DateTime.Now.ToString();
this._tablecell.InnerHtml = "";
this.AddControls();
}
/// <summary>
/// 重写Render
/// </summary>
protected override void Render(HtmlTextWriter writer)
{
base.Render(writer);
}
private void AddControls()
{
this.lb.ID = this.ID + "Label";
this.lb.Text = this._text;
this._tablecell.Controls.Add(this.lb);
this._tablecell.Controls.Add(new LiteralControl("<br>"));
this.btn.ID = this.ID + "Button";
this.btn.Text = "获取当前时间:";
this._tablecell.Controls.Add(this.btn);
}
}
}
按照上文的部署方法,将GetNow的WebPart添加到SharePoint网站中:测试一下,获取时间成功!
Demo 2
第二个例子我们来实现WebPart的联动控制,也就是说通过一个WebPart来控制另外一个WebPart。还是直接看代码:
![](https://i-blog.csdnimg.cn/blog_migrate/0be121fa5b8988fbabbbc526af3b0fc0.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/34031c708bfe702fe82d01ff5c6593aa.gif)
using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;
using System.Collections;
using System.Drawing;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
using GetNow;
namespace SetColor
{
[Guid("803617a2-9dc9-4eb8-b05c-3e064bc16642")]
public class SetColor : System.Web.UI.WebControls.WebParts.WebPart, IPostBackEventHandler
{
private IList _colorlist = new ArrayList();
private HtmlTableCell _TableCell = new HtmlTableCell();
public SetColor()
{
this.GetItems();
this.ExportMode = WebPartExportMode.All;
}
private void GetItems()
{
this._colorlist.Add("红色");
this._colorlist.Add("黄色");
this._colorlist.Add("蓝色");
this._colorlist.Add("绿色");
this._colorlist.Add("紫色");
}
private void AddRows()
{
for (int i = 0; i < _colorlist.Count; i++)
{
this._TableCell.Controls.Add(new LiteralControl("<table>" + "/n"));
this._TableCell.Controls.Add(new LiteralControl(" <tr>" + "/n"));
this._TableCell.Controls.Add(new LiteralControl(" <td style=/"cursor:hand/" οnclick=/"" + this.Page.ClientScript.GetPostBackEventReference(this, this._colorlist[i].ToString()) + "/">" + this._colorlist[i].ToString() + "</td>" + "/n"));
this._TableCell.Controls.Add(new LiteralControl(" </tr>" + "/n"));
this._TableCell.Controls.Add(new LiteralControl("</table>" + "/n"));
}
}
protected override void CreateChildControls()
{
this.Controls.Add(new LiteralControl("<table>" + "/n"));
this.Controls.Add(new LiteralControl(" <tr>" + "/n"));
this.Controls.Add(this._TableCell);
this.Controls.Add(new LiteralControl(" </tr>" + "/n"));
this.Controls.Add(new LiteralControl("</table>" + "/n"));
if (this.Page.IsPostBack == false)
{
this.AddRows();
}
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
if (this.Page.IsPostBack == true)
{
this.AddRows();
}
}
protected override void Render(HtmlTextWriter writer)
{
base.Render(writer);
}
public void RaisePostBackEvent(string eventArgument)
{
for (int i = 0; i < WebPartManager.WebParts.Count; i++)
{
if (WebPartManager.WebParts[i].Title == "GetNow")
{
GetNow.GetNow instance = (GetNow.GetNow)WebPartManager.WebParts[i];
switch (eventArgument)
{
case "红色":
instance.BackColor = Color.Red;
break;
case "黄色":
instance.BackColor = Color.Yellow;
break;
case "蓝色":
instance.BackColor = Color.Blue;
break;
case "绿色":
instance.BackColor = Color.Green;
break;
case "紫色":
instance.BackColor = Color.Purple;
break;
default:
break;
}
}
}
}
}
}
代码不是很难,创建一个WebPart,向其中添加一个List,提供用户几种颜色的选择情况,然后通过用户选择的情况改变我们上面GetNow这个WebPart的背景颜色。
按照前面的方法加到SharePoint网站中:
选择红色:
选择紫色:
ok。我们想要的效果已经实现啦!
小结:上面只是自己抛砖引玉的小例子,希望对新手有帮助。对于WebPart的开发部分就介绍这些吧,望支持。有什么不得当的地方还望大家指教。根据自己的业务需要,大家可以开发出更实用的WebPart。还有建议大家多看看服务器控件开发的文章,会有帮助!