1 / 12

WalkThrough SharePoint WebPart 入门指南 三

WalkThrough SharePoint WebPart 入门指南 三. blog.joycode.com. Kaneboy [MS MVP]. 转载声明:此 WalkThrough 系列被转载和引用时,请保持博客堂链接。 Thanks. 将一个 Code-Behind 方式编写的 User Control 载入到 WebPart 中 将所有的内容( WebPart 、 User Control 、包括 .ascx )集成到 WebPart 所在的一个 Assembly 中,以简化部署. 此 WalkThrough 将演示.

michon
Download Presentation

WalkThrough SharePoint WebPart 入门指南 三

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. WalkThroughSharePoint WebPart 入门指南 三 blog.joycode.com Kaneboy [MS MVP] 转载声明:此WalkThrough系列被转载和引用时,请保持博客堂链接。 Thanks.

  2. 将一个Code-Behind方式编写的User Control载入到WebPart中 将所有的内容(WebPart、User Control、包括.ascx)集成到WebPart所在的一个Assembly中,以简化部署 此WalkThrough将演示

  3. 请通过此WalkThrough系列的前两辑了解在VS.NET中创建WebPart、加入User Control、简单部署WebPart等基础知识。 如果希望深入了解此WalkThrough中演示的部分代码的含义,请参看MSDN中有关Assembly中Resource部分的文档(如果不想了解,照葫芦画瓢亦可)。 背景知识

  4. 一、创建用户控件 创建一个“ASP.NET Web应用程序”项目,在项目中添加一个名为“WebUserControl1”的用户控件,并放入一个Calendar控件。 在User Control的Code-Behind文件(这里是“WebUserControl1.ascx.cs”)中,加入自己想加入的代码。我们的示例中在User Control的Page_Load事件中加入了一行代码,用来设置Calendar控件的ToolTip属性

  5. 二、创建WebPart项目 在VS.NET中重新建立一个类型为“Web Part Library”,名称为“SampleWebPart”的项目。 在项目中通过“添加现有项…”菜单,将第一步中建立的UserControl的三个文件全部加入到此项目中。 打开“WebUserControl1.ascx.cs”文件,将此文件第一行指定的namespace改成和此项目一致的“SampleWebPart”: namespace SampleWebPart 打开“WebUserControl1.ascx”文件,将第一行的“<%@ Control>”标签中的“CodeBehind”属性删除,“Inherits”属性的值改为“SampleWebPart.WebUserControl1”: <%@ Control Language="c#" AutoEventWireup="false" Inherits="SampleWebPart.WebUserControl1" TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>

  6. 三、设置User Control的编译目标 在“解决方案资源管理器”中选中User Control项“WebUserControl1.ascx”,点击鼠标右键,选择“属性”菜单。 在出现的属性窗口中,将“生成操作”属性的值更改为“嵌入的资源”。

  7. 四、在WebPart中载入Resource信息并输出 进行这一步骤之前,请回忆WalkThrough第一辑中的第二步。我们这里在WebPart中嵌入一个UserControl的原理和第一辑中的基本相同。 让我们现在转到“WebPart1.cs”文件,开始修改我们的WebPart。 1、定义一个用来保存UserControl的对象: private System.Web.UI.Control _innerControl;

  8. 四、在WebPart中载入Resource信息并输出(二) 前面两行的意思是从当前Assembly的Resource中取出指定信息并读到变量resourceContent中。 2、重载CreateChildControls()方法。 protected override void CreateChildControls() { using (StreamReader reader = new StreamReader(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("SampleWebPart.WebUserControl1.ascx"))) { String resourceContent = reader.ReadToEnd(); using (StreamWriter writer = new StreamWriter(this.Page.Server.MapPath("/bin/WebUserControl1.ascx"), false)) { writer.Write(resourceContent); } } this._innerControl = this.Page.LoadControl("/bin/WebUserControl1.ascx"); this.Controls.Add(this._innerControl); } 这两行的意思是将resourceContent中的内容写入到一个名为“WebUserControl1.ascx”的文件中。

  9. 四、在WebPart中载入Resource信息并输出(三) 3、在RenderWebPart()方法中输出载入的User Control: protected override void RenderWebPart(HtmlTextWriter output) { this.EnsureChildControls(); this._innerControl.RenderControl(output); }

  10. 五、WebPart的其他工作 1、编辑“WebPart1.dwp”,配置WebPart。 2、编辑SPS虚拟站点上的“web.config”,添加“<SafeControl>”标签以信任我们的WebPart。 3、在VS.NET中编译,生成最终的“SampleWebPart.dll”。将此(.dll)拷贝到SPS虚拟站点跟目录的“bin”目录下。(只需要拷贝这一个(.dll)文件,不需要再另行拷贝(.ascx)文件。) 4、在SPS站点页面中导入我们这个WebPart。 上面的步骤详细操作请参看此WalkThrough第一辑中的第三、四、五、六步骤。

  11. 六、完成 WebPart在页面上面的效果如左图。 当把鼠标移到Calendar控件上时,可以看到有“Sample Calendar”的提示信息,验证了在第一步添加的Code-Behind代码被执行了。

  12. 七、To Be Continued… 注意: 我们前面的步骤仅仅用来演示如何实现我们需要的效果,但其细节代码实现的方式并不推荐。 首先不推荐将临时的“WebUserControl1.ascx”文件直接写到SPS虚拟站点的“bin”目录中(站点根目录下的“wpresources”是一个不错的选择),其次在WebPart中不应该每次都从Resource中读出数据并写到临时文件,而应该适当的应用缓存。 下一辑: 将演示用(.CAB)方式和(.MSI)方式在SPS服务器上部署WebPart。

More Related