webpart制作教程
sharepoint2003的webpart与sharpoint2007的webpart制作有所区别。这里主要讲sharpoint2007的webpart制作。
步骤:
1、安装vs2005.
2、安装webpart模板:VSeWSS.exe
3、创建项目,选择web part.如下图:
4、创建完成后,会自动生成如下代码:
5、webpart添加到页面后要显示哪些内容?这个在
protected override void Render(HtmlTextWriter writer)
{
// TODO: add custom rendering code here.
// writer.Write("Output HTML");
}
中实现。如果我要在页面上显示“你好!”,只需在上述方法中输入:writer.Write("你好!");
注意:Write();中输入的HTML代码其实是放在一个DIV中的。
6、如果要在webpart中加入一些控件怎么办呢?
这时候就需要重载CreateChildControls()方法了。
首先New一个你需要的控件。例如下拉框:
private DropDownList _DropDownList = new DropDownList();
protected override void CreateChildControls()
{
this.Controls.Add(this._DropDownList);
}
然后在Render中输出就行了。
protected override void Render(HtmlTextWriter writer)
{
// TODO: add custom rendering code here.
// writer.Write("Output HTML");
base.Render(writer);
}
7、如果控件是有事件处理的,那该怎么办呢?
public Web_Part2()
{
this.ExportMode = WebPartExportMode.All;
this._DropDownList.AutoPostBack = true;
this._DropDownList.SelectedIndexChanged += new EventHandler(_DropDownList_SelectedIndexChanged);
}
void _DropDownList_SelectedIndexChanged(object sender, EventArgs e)
{
//TODO:你的事件处理代码
}
如此便OK啦。
步骤:
1、安装vs2005.
2、安装webpart模板:VSeWSS.exe
3、创建项目,选择web part.如下图:
4、创建完成后,会自动生成如下代码:
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 Web_Part2
{
[Guid("aa39918b-74b7-48ec-b7d2-ffcbf7e4da94")]
public class Web_Part2 : System.Web.UI.WebControls.WebParts.WebPart
{
public Web_Part2()
{
this.ExportMode = WebPartExportMode.All;
}
protected override void Render(HtmlTextWriter writer)
{
// TODO: add custom rendering code here.
// writer.Write("Output HTML");
}
}
}
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 Web_Part2
{
[Guid("aa39918b-74b7-48ec-b7d2-ffcbf7e4da94")]
public class Web_Part2 : System.Web.UI.WebControls.WebParts.WebPart
{
public Web_Part2()
{
this.ExportMode = WebPartExportMode.All;
}
protected override void Render(HtmlTextWriter writer)
{
// TODO: add custom rendering code here.
// writer.Write("Output HTML");
}
}
}
5、webpart添加到页面后要显示哪些内容?这个在
protected override void Render(HtmlTextWriter writer)
{
// TODO: add custom rendering code here.
// writer.Write("Output HTML");
}
中实现。如果我要在页面上显示“你好!”,只需在上述方法中输入:writer.Write("你好!");
注意:Write();中输入的HTML代码其实是放在一个DIV中的。
6、如果要在webpart中加入一些控件怎么办呢?
这时候就需要重载CreateChildControls()方法了。
首先New一个你需要的控件。例如下拉框:
private DropDownList _DropDownList = new DropDownList();
protected override void CreateChildControls()
{
this.Controls.Add(this._DropDownList);
}
然后在Render中输出就行了。
protected override void Render(HtmlTextWriter writer)
{
// TODO: add custom rendering code here.
// writer.Write("Output HTML");
base.Render(writer);
}
7、如果控件是有事件处理的,那该怎么办呢?
public Web_Part2()
{
this.ExportMode = WebPartExportMode.All;
this._DropDownList.AutoPostBack = true;
this._DropDownList.SelectedIndexChanged += new EventHandler(_DropDownList_SelectedIndexChanged);
}
void _DropDownList_SelectedIndexChanged(object sender, EventArgs e)
{
//TODO:你的事件处理代码
}
如此便OK啦。