存储在数据库中的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" %>