MOSS AJAX WebParts开发环境设置

 

置顶:MOSS SP1开始支持AJAX。首先确认你的MOSS版本号

想必开发过MOSS AJAX控件的都知道,为了实现AJAX,必须对MOSS的环境进行大量的设置。网上有大量此类的文章,其中最流行的做法应该是刊登在moss开发团队blog上的(点击下面的URL进入)。

http://blogs.msdn.com/sharepoint/archive/2007/03/02/integrating-asp-net-ajax-with-sharepoint.aspx

简单概括一下步骤的话:

1.    下载并安装ASP.NET AJAX http://www.asp.net/ajax/

2.    扩展web.config使其支持ASP.NET AJAX

3.    master pager上添加ASP.NET AJAX Script Manager

 

算得上是官方推荐的做法吧,只是有点麻烦,好在对MOSS进行AJAX扩展的人越来越多,网上出现了很多简便的做法,下面是鄙人的心得

1.    下载并安装ASP.NET AJAX

2.    下载并安装stsadm.ajaxifymoss自动配置web.config

3.    WebpartsOnInit事件里添加ASP.NET AJAX Script Manager

 

接下来对各个步骤进行介绍

1       下载并安装ASP.NET AJAX http://www.asp.net/ajax/

推荐安装ASP.NET AJAX Extensions 1.0版本。

2       下载并安装stsadm.ajaxifymoss自动配置web.config
http://www.codeplex.com/ajaxifymoss/Release/ProjectReleases.aspx?ReleaseId=13360

2.1      下载StsAdm.AjaxifyMoss.zip并解压,执行deploy.cmd后执行upgrade.cmdStsAdm.AjaxifyMoss.zip必须在同一个文件夹内)

2.2      启动dos命令栏窗口(cmd),使用stsadm运行ajaxifying(默认在C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN
全部命令如下

功能 使用例
add asp.net ajax to web.config stsadm -o addajax -url http://your.moss.webapplication
remove asp.net ajax from web.config stsadm -o removeajax -url http://your.moss.webapplication
add .Net 3.5 ajax to web.config stsadm -o addajax35 -url http://your.moss.webapplication
remove .Net 3.5 ajax from web.config stsadm -o removeajax35 -url http://your.moss.webapplication

      

3       WebpartsOnInit事件里添加ASP.NET AJAX Script Manager(参考示例代码)

4       运行例代码(不要忘记添加System.Web.Extensions)
(如果运行成功并且不发生全页面刷新现象,那么就说明配置成功。WebParts开发使用Visual Studio 2005 Extensions, Version 1.12008请使用V1.2)

 

using System;

using System.Collections;

using System.Text;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI;

using System.Runtime.InteropServices;

using Microsoft.SharePoint;

using Microsoft.SharePoint.WebControls;

using Microsoft.SharePoint.WebPartPages;

 

namespace UsesUpdatePanel

{

    [Guid("4e393abc-cc9c-4182-8d32-a742f6ace07b")]

    public class AjaxUpdatePanelPart : System.Web.UI.WebControls.WebParts.WebPart

    {

        private Label label;

        private TextBox textBox;

        private UpdatePanel up;

        private Button button;

        private ScriptManager _AjaxManager;

 

        //自定一个ScriptManager用来存储获取的ScriptManager

        [WebPartStorage(Storage.None)]

        public ScriptManager AjaxManager

        {

            get { return _AjaxManager; }

            set { _AjaxManager = value; }

        }

       

        //面初始化事件

        protected override void OnInit(EventArgs e)

        {

            base.OnInit(e);

            //面上的ScriptManager

            _AjaxManager = ScriptManager.GetCurrent(this.Page);

 

            //如果面上没有ScriptManager追加一个

            if (_AjaxManager == null)

            {

                _AjaxManager = new ScriptManager();

                _AjaxManager.EnablePartialRendering = true;

 

                Page.ClientScript.RegisterStartupScript(

                    typeof(AjaxUpdatePanelPart),

                    this.ID,

                    "_spOriginalFormAction = document.forms[0].action;",

                    true);

               

                if (this.Page.Form != null)

                {

                    string formOnSubmitAtt = this.Page.Form.Attributes["onsubmit"];

                    if (!string.IsNullOrEmpty(formOnSubmitAtt)

                        && formOnSubmitAtt == "return _spFormOnSubmitWrapper();")    

                    {

                        this.Page.Form.Attributes["onsubmit"] = "_spFormOnSubmitWrapper();";

                    }

                    this.Page.Form.Controls.AddAt(0, _AjaxManager);

                }

            }

        }

 

        //建各个控件,并填充UpdatePanel

        protected override void CreateChildControls()

        {

            base.CreateChildControls();

            this.EnsureUpdatePanelFixups();

            up = new UpdatePanel();

            up.ID = "UpdatePanel1";

            up.ChildrenAsTriggers = true;

            up.RenderMode = UpdatePanelRenderMode.Inline;

            up.UpdateMode = UpdatePanelUpdateMode.Always;

            this.textBox = new TextBox();

            this.textBox.ID = "TextBox";

            up.ContentTemplateContainer.Controls.Add(this.textBox);

            this.label = new Label();

            this.label.Text = "Enter your name.";

            up.ContentTemplateContainer.Controls.Add(this.label);

            this.button = new Button();

            button.CausesValidation = false;

            button.ID = "Button1";

            button.Text = "Say Hello";

            button.Click += new EventHandler(HandleButtonClick);

            up.ContentTemplateContainer.Controls.Add(this.button);

            _AjaxManager.RegisterAsyncPostBackControl(this.button);

            this.Controls.Add(up);

        }

 

        private void HandleButtonClick(object sender, EventArgs eventArgs)

        {           

            this.label.Text = "Hello " + this.textBox.Text;

        }

 

        /*MOSS中,了正确解析某些特殊的URLs,例如包含字等两比特字符的URL

         * Windows SharePoint Services JavaScript使用form onSubmit wrapper重form action

         * 下面个方法用来恢form action,如果你的URL里不包含字等双字的字符,

         * ,恭喜,你可以使用ASP.NET AJAX UpdatePanels

         * (具体参考上文中提到的moss开发团队blog」) */

        private void EnsureUpdatePanelFixups()

        {

            if (this.Page.Form != null)

            {

                string formOnSubmitAtt = this.Page.Form.Attributes["onsubmit"];

 

                if (formOnSubmitAtt == "return _spFormOnSubmitWrapper();")

                {

                    this.Page.Form.Attributes["onsubmit"] = "_spFormOnSubmitWrapper();";

                }

 

            }

            ScriptManager.RegisterStartupScript(

                this,

                typeof(AjaxUpdatePanelPart),

                "UpdatePanelFixup",

                "_spOriginalFormAction = document.forms[0].action; _spSuppressFormOnSubmitWrapper=true;",

                true);

        } 

    } 

}

 

posted on 2009-02-26 15:17  阿米巴原虫  阅读(862)  评论(0编辑  收藏  举报