我正在使用Visual Studio 2008,并已下载VSeWSS.exe 1.2,以启用Web部件开发。我是SP开发的新手,我已经对不同版本的SP和VS附加组件的数量感到困惑。这个特殊的问题出现了,这凸显了我的困惑。
我选择了添加 - >新项目 - > Visual C# - > SharePoint的> Web部件,接受默认值,VS创建了一个项目,主文件为WebPart1.cs
using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
namespace WebPart1
{
[Guid("9bd7e74c-280b-44d4-baa1-9271679657a0")]
public class WebPart1 : System.Web.UI.WebControls.WebParts.WebPart
{
public WebPart1()
{
}
protected override void CreateChildControls() // <-- This line
{
base.CreateChildControls();
// TODO: add custom rendering code here.
// Label label = new Label();
// label.Text = "Hello World";
// this.Controls.Add(label);
}
}
}
我正在关注的书,由Jeff Webb撰写的Essential SharePoint 2007,对于默认项目有以下内容 -
using System;
<...as previously>
namespace WebPart1
{
[Guid("9bd7e74c-280b-44d4-baa1-9271679657a0")]
public class WebPart1 : System.Web.UI.WebControls.WebParts.WebPart
{
// ^ this is a new style (ASP.NET) web part! [author's comment]
protected override void Render(HtmlTextWriter writer) // <-- This line
{
// This method draws the web part
// TODO: add custom rendering code here.
// writer.Write("Output HTML");
}
}
}
我担心的真正原因是,在本书的这一章中,作者经常提到“旧式”网页部分与“新式”网页部分之间的区别,正如他在Render方法评论中所指出的那样。 / p>
发生了什么事?为什么我的默认Web部件与作者签名不同?
答案 0 :(得分:0)
作者与“新风格”Web部件的区别在于它们是ASP.NET 2.0 Web部件(2005年发布),可以在SharePoint和ASP.NET中使用。旧式Web部件特定于SharePoint
在问题的代码示例中,两个Web部件都是新的样式,即。它们是ASP.NET Web部件。唯一的区别是视觉工作室覆盖了与书不同的方法。然而,这两种方法,以及许多其他方法,例如。 OnLoad,OnInit可用,并将自定义以使Web部件工作。
经过几个月的Web部件开发,我的建议是使用第一个作为“hello world”Web部件的基础,即。
protected override void CreateChildControls()
{
base.CreateChildControls();
Label label = new Label();
label.Text = "Hello World";
this.Controls.Add(label);
}
然后开始向此方法添加代码,或添加其他方法(例如OnLoad,OnPrerender)以添加功能。
Render 方法在大多数网页部件中都不会覆盖。