对SharePoint进行扩充的一个最好的方法就是开发自己的Web部件,但是在开发Web部件之前首先要下载Web Part Templates for Visual Studio .NET:(http://www.microsoft.com/downloads/details.aspx?FamilyId=14D5D92F-C3A6-407C-AAD7-B8C41A4991BE&displaylang=en)。
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 MyWebParts
{
/// <summary>
/// Description for WebPart1.
/// </summary>
[DefaultProperty("Text"),
ToolboxData("<{0}:WebPart1 runat=server></{0}:SimpleWebPart>"),
XmlRoot(Namespace="MyWebParts")]
public class SimpleWebPart : Microsoft.SharePoint.WebPartPages.WebPart
{
protected TextBox txtValue1;
protected TextBox txtValue2;
protected TextBox txtResult;
protected Label lblOp;
protected Label lblEq;
protected Button btnCalc;
//private const string defaultText = "";
//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;
// }
// }
private bool calcEnabled = true;
[Browsable(true),
Description("是否启用计算功能!"),
WebPartStorage(Storage.Personal),
FriendlyName("允许计算"),
DefaultValue(true),
Category("自定义")
]
public bool CalcEnabled
{
get
{
return calcEnabled;
}
set
{
calcEnabled = value;
if( this.btnCalc != null )
{
this.btnCalc.Enabled = value;
}
}
}
/// <summary>
/// This method gets the custom tool parts for this Web Part by overriding the
/// GetToolParts method of the WebPart base class. You must implement
/// custom tool parts in a separate class that derives from
/// Microsoft.SharePoint.WebPartPages.ToolPart.
/// </summary>
///<returns>An array of references to ToolPart objects.</returns>
// public override ToolPart[] GetToolParts()
// {
// ToolPart[] toolparts = new ToolPart[2];
// WebPartToolPart wptp = new WebPartToolPart();
// CustomPropertyToolPart custom = new CustomPropertyToolPart();
// custom.Expand("自定义");
// toolparts[0] = custom;
// toolparts[1] = wptp;
// return toolparts;
// }
/// <summary>
/// Render this Web Part to the output parameter specified.
/// </summary>
/// <param name="output"> The HTML writer to write out to </param>
protected override void RenderWebPart(HtmlTextWriter output)
{
//output.Write(SPEncode.HtmlEncode(Text));
EnsureChildControls();
output.RenderBeginTag("div");
//RenderChildren(output);
this.txtValue1.RenderControl(output);
output.AddStyleAttribute(HtmlTextWriterStyle.FontWeight,"bold");
output.RenderBeginTag("span");
this.lblOp.RenderControl(output);
output.RenderEndTag(); //span