在vs.net里开发WebPart,首先需要下载一个WebPartTemplate
1>打开Vs.Net-->新建项目-->选择项目类型(使用哪种语言)--->选择WebPart Library模板,项目名称
这里取:Zhangzs.WebParts--->确定
默认会建立一个WebPart1的WebPart.包含一个WebPart1.cs和WebPart1.dwp文件.暂时不理会它.
2>右键单击项目-->添加(添加新项)-->模板选择:WebPart,并为其取一个名字,这里叫FirstWebPart
FirstWebPart.cs代码:
3>增加dwp文件,注意名称一定要对应,这里必须为FirstWebPart.dwp.代码如下,注意Assembly,TypeName
1>打开Vs.Net-->新建项目-->选择项目类型(使用哪种语言)--->选择WebPart Library模板,项目名称
这里取:Zhangzs.WebParts--->确定
默认会建立一个WebPart1的WebPart.包含一个WebPart1.cs和WebPart1.dwp文件.暂时不理会它.
2>右键单击项目-->添加(添加新项)-->模板选择:WebPart,并为其取一个名字,这里叫FirstWebPart
FirstWebPart.cs代码:
using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Serialization;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.WebPartPages;
namespace Zhangzs.WebParts
{
/// <summary>
/// Description for FirstWebPart
/// </summary>
[DefaultProperty("Text"),
ToolboxData("<{0}:FirstWebPart runat=server></{0}:FirstWebPart>"),
XmlRoot(Namespace="Zhangzs.WebParts")]
public class FirstWebPart : Microsoft.SharePoint.WebPartPages.WebPart
{
private const string defaultText = "Hello Wold";
private string text = defaultText;
[Browsable(true),
Category("Miscellaneous"),
DefaultValue(defaultText),
WebPartStorage(Storage.Personal),
FriendlyName("Text"),
Description("Text Property")]
public string Text
{
get
{
return text;
}
set
{
text = value;
}
}
protected override void RenderWebPart(HtmlTextWriter output)
{
output.Write(SPEncode.HtmlEncode(Text));
}
}
}
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Serialization;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.WebPartPages;
namespace Zhangzs.WebParts
{
/// <summary>
/// Description for FirstWebPart
/// </summary>
[DefaultProperty("Text"),
ToolboxData("<{0}:FirstWebPart runat=server></{0}:FirstWebPart>"),
XmlRoot(Namespace="Zhangzs.WebParts")]
public class FirstWebPart : Microsoft.SharePoint.WebPartPages.WebPart
{
private const string defaultText = "Hello Wold";
private string text = defaultText;
[Browsable(true),
Category("Miscellaneous"),
DefaultValue(defaultText),
WebPartStorage(Storage.Personal),
FriendlyName("Text"),
Description("Text Property")]
public string Text
{
get
{
return text;
}
set
{
text = value;
}
}
protected override void RenderWebPart(HtmlTextWriter output)
{
output.Write(SPEncode.HtmlEncode(Text));
}
}
}
3>增加dwp文件,注意名称一定要对应,这里必须为FirstWebPart.dwp.代码如下,注意Assembly,TypeName
4>修改MainFest.xml文件,在DwpFiels节下增加 <DwpFile FileName="FirstWebPart.dwp"/>