为MOSS页面添加后台代码的两种方式(一)

      我们知道,存储在数据库里的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
ExpandedBlockStart.gifContractedBlock.gif {
    public class CodeBehind: PublishingLayoutPage 
ExpandedSubBlockStart.gifContractedSubBlock.gif    {
    }

}

     这样我们就可以为页面上的控件添加相应的后台代码。比方说我们的页面上有一个按钮和一个文本框,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
ExpandedBlockStart.gifContractedBlock.gif {
 public class CodeBehind: PublishingLayoutPage
ExpandedSubBlockStart.gifContractedSubBlock.gif {
   protected TextBox textbox1;
     protected Button button1;
   protected override void OnInit(EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif     {
         base.OnInit(e);
         button1.Click += new EventHandler  (button1_Click);
        }

 }

 void button1_Click(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif {
     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-06-17 15:16 鱼缸里的鲸鱼 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/fatwhale/archive/2008/06/17/1224011.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值