officeba > 单独文章


给SharePoint页面添加后台代码

我们知道,存储在数据库里的SharePoint页面是不能直接添加后台代码的,这给我们带来了很多的不方便,比如想要在页面上实现一些东西,都必须使用Webpart或者自定义控件的方式,哪怕仅仅是很简单的几行后台代码。而WSS 3.0 是基于ASP.NET 2.0的,在ASP.NET站点里使用的任何技术在WSS站点里同样可以使用。因此我们同样可以给WSS站点的页面添加后台代码。

存储在数据库中的sharepoint页面分为两部门,母板页和内容页,我们可以为这两种页面分别添加后台代码。实现方式不一样,若为内容页添加后台代码,我们需要继承自Microsoft.SharePoint.Publishing.PublishingLayoutPage类,若为母板页添加后台代码,我们需要继承自System.Web.UI.MasterPage类,你应该将后台代码类与对应页面设置成相同的名字,但这不是必须的。如下所示:
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Publishing;
using Microsoft.SharePoint.WebControls;
namespace MossCodeBehind{
public class CodeBehind: PublishingLayoutPage {
}
}
     这样我们就可以为页面上的控件添加相应的后台代码。比方说我们的页面上有一个按钮和一个文本框,ID分别为textbox1和button1,并为button添加一个ckick事件,当点击按钮时,将当前时间写入文本框中,可以这么来写:
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Publishing;
using Microsoft.SharePoint.WebControls;
namespace MossCodeBehind
{
public class CodeBehind: PublishingLayoutPage
 {
   protected TextBox textbox1;
        protected Button button1;
   protected override void OnInit(EventArgs e)
      {
            base.OnInit(e);
            button1.Click += new EventHandler(button1_Click);
        }
}
void button1_Click(object sender, EventArgs e)
{
     textbox1.Text = DateTime.Now.ToString();
}
}
   在MOSS的页面上,服务器控件分为ASP控件(命名空间System.Web.UI.WebControls)和sharepoint控件(命名空间是Microsoft.SharePoint.WebControls),我们同样可以声明sharepoint控件并为它们添加相应的操作。
写好我们的后台代码后,将代码生成到对应的bin目录下(或者GAC,记得强命名),在web.config文件中添加一行,<SafeControl Assembly="" Namespace="" TypeName="*" Safe="True" />,其中assembly和namespace可以通过reflector获得,然后我们还需要在页面上重写页:
<%@ Page meta:progid="SharePoint.WebPartPages.Document" Language="C#" Inherits="MossCodeBehind.CodeBehind,MossCodeBehind, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" %>
如果是母板页,这样添加
<%@ Master language="C#" Inherits=" MossCodeBehind.CodeBehind,MossCodeBehind, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" %>

不过重写了之后,就不能在设计窗口中打开页面了。

通过这种方式,开发者就可以在像ASP.NET中一样做开发,例如我们可以重写onload事件来实现向页面的控件绑定数据。
有兴趣的朋友可以尝试一下,能满足我们很多的需求。

作者:whalelover

声明:欢迎各大网站转载本站文章,还请保留一条能直接指向本站的超级链接,谢谢!

时间:2008-06-25 21:35:07,点击:65824


【OfficeBa论坛】:阅读本文时遇到了什么问题,可以到论坛进行交流!Excel专家邮件:342327115@qq.com(大家在Excel使用中遇到什么问题,可以咨询此邮箱)。

【声明】:以上文章或资料除注明为Office自创或编辑整理外,均为各方收集或网友推荐所得。其中摘录的内容以共享、研究为目的,不存在任何商业考虑。如有任何异议,请与本站联系,本站确认后将立即撤下。谢谢您的支持与理解!


相关评论

我要评论

评论内容