承接MOSS各种工作流开发 联系人:王先生.电话:18618405729QQ:252385878 QQ群:41696750 MSN:wanghao-3@hotmail.com

导航

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 AA{
public class AAClass: 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 AA
{
public class AAClass: 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事件来实现向页面的控件绑定数据。
有兴趣的朋友可以尝试一下,能满足我们很多的需求。

 

 

posted on 2008-09-11 21:45  A A  阅读(986)  评论(1编辑  收藏  举报