SharePoint 2007图文开发教程(4) 实现高级的WebPart
如果大家有开发服务器控件的基础,此文对你来说没有什么难度。想了解相关内容,参看我的《ASP.NET的服务器控件开发》系列。废话不多说,直接进入今天的主题。
Demo 1
第一个例子也很简单,点击一个按钮,显示当前系统时间。如何创建WebPart就不细说了,不清楚的朋友可以参看《SharePoint 2007图文开发教程(3)---实现简单的WebPart》。
直接看代码:
- 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。还是直接看代码:
- 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\" onclick=\"" + 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。还有建议大家多看看服务器控件开发的文章,会有帮助!
相关文章
SharePoint 2007图文开发教程(8) Excel Services扫盲
SharePoint 2007中,Excel Services是不得不提的,它是 Microsoft Office SharePoint Server 2007 中包含的一项新的服务器技术2015-12-29SharePoint 2007图文开发教程(7) 在SharePoint中实现Workflow
继续我们的SharePoint之旅,体验Workflow的应用,由于以前也很少弄WF,望有什么问题WF的高手多多指教2015-12-29SharePoint 2007图文开发教程(6) 实现Search Services
SharePoint中为我们提供了Search的功能,和Event Handler的实现比较类似,还是需要启动支持,定制服务,运行服务几部分内容,下面逐一说下2015-12-29SharePoint 2007图文开发教程(5) 体验Event Handler
前面两篇文章介绍了有关WebPart的相关内容,此文继续SharePoint之旅,体验下Event Handler的相关内容,需要的朋友可以参考下2015-12-29SharePoint 2007图文开发教程(3) 实现简单的WebPart
在前面的文章中,我们讲解了很多基础的内容,主要包括 安装配置、简单的创建网站等。相对来说比较简单。此文实现简单的WebPart,继续我们的SharePoint之旅2015-12-29SharePoint 2007图文开发教程(2) 使用SharePoint创建网站的步骤
上文简单介绍了有关SharePoint的安装,配置等内容的介绍。此文我们继续SharePoint之旅,通过SharePoint创建网站,需要的朋友可以参考下2015-12-29SharePoint 2007图文开发教程(1) 简介,安装,配置及创建Web应用程序
Microsoft Office SharePoint Server 2007 是一个服务器功能集成套件,它提供全面的内容管理和企业搜索、加速共享业务流程并便利跨界限信息共享以更好地了解业务,从而有助2015-12-29SharePoint Designer 2007 官方简体中文版
Frontpage 2003的下一个版本,不过这个版本绝对不是Frontpage 2003的简单改进版,Microsoft Office SharePoint Designer 2007 是一种全新的Web2.0产品,用于基于 SharePoin2015-12-29Microsoft Office SharePoint Server 2007 SDK软件开发工具包 简体中文
为了让解决方案提供商、独立软件供应商、增值经销商以及其他开发人员了解全新的 Office SharePoint Server 2007 企业应用程序和平台2015-03-25亮剑.NET:SharePoint Server 2007开发实战 (张建义,王俞) pdf扫描版
本书共分为8章,详细讲解了sharepoint上常见的开发任务,讲述了各种开发场景下需要了解的知识,并提供了丰富的实例2013-10-27
最新评论