MOSS AJAX WebParts开发

(一) moss Ajax 配置

  1. Microsoft SharePoint Team Blog 的方案,Integrating ASP.NET AJAX with SharePoint

  2.使用stsadm.ajaxifymoss配置的方案 MOSS AJAX WebParts开发环境设置

  3. stsAdm.Ajaxify解决方案下载 Ajaxify MOSS

   大致就是修改应用程序web.config文件,大致有7处

(二)一个简单的ajax hello的WebPart

  1.首先开发一个基类的WebPart来检查注册ScriptManager等 

代码
using  System;
using  System.Collections.Generic;
using  System.Text;
using  System.Web.UI.WebControls.WebParts;
using  Microsoft.SharePoint.WebPartPages;
using  System.Web.UI;

public   class  BaseAjaxWebPart : System.Web.UI.WebControls.WebParts.WebPart
{
    
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 (BaseAjaxWebPart),  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);
            }
        }
    }

    
/* 在MOSS中,为了正确解析某些特殊的URLs,例如包含汉字等两比特字符的URL,
      * Windows SharePoint Services JavaScript使用form onSubmit wrapper重载默认的form action,
      * 下面这个方法用来恢复默认的form action,如果你的URL里不包含汉字等双字节的字符,
      * 那么,恭喜,你可以使用ASP.NET AJAX UpdatePanels
      * (具体请参考上文中提到的「moss开发团队blog」) 
*/
    
protected   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 (BaseAjaxWebPart),
            
" UpdatePanelFixup " ,
            
" _spOriginalFormAction = document.forms[0].action; _spSuppressFormOnSubmitWrapper=true; " ,
            
true );
    }

    
protected   void  RegisterAsyncPostBackControl(Control control)
    {
        
if  (control  !=   null )
        {
            _AjaxManager.RegisterAsyncPostBackControl(control);
        }
    }

    
protected   override   void  CreateChildControls()
    {
        
base .CreateChildControls();
        
this .EnsureUpdatePanelFixups();
    }
}

2. helloWebPart代码

代码
using  System;
using  System.Collections.Generic;
using  System.Text;
using  System.Web;
using  System.Web.UI;
using  System.Web.UI.WebControls;
using  System.Web.UI.WebControls.WebParts;
using  Microsoft.SharePoint.WebPartPages;

namespace  WebPart.Test
{
    
public   class  AjaxSayHelloWebpart : BaseAjaxWebPart
    {
        
private  Label label;
        
private  TextBox textBox;
        
private  UpdatePanel up;
        
private  Button button;
        
// 创建各个控件,并填充UpdatePanel

        
protected   override   void  CreateChildControls()
        {
            
base .CreateChildControls();         
            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);
            
base .RegisterAsyncPostBackControl( this .button);
            
this .Controls.Add(up);
        }

        
private   void  HandleButtonClick( object  sender, EventArgs eventArgs)
        {
            
this .label.Text  =   " Hello  "   +   this .textBox.Text;
        }

    }
}

 

3. 打包发布后,

 你在输入框输入 "xxxx",点击say hello的按钮,label就会显示 "hello xxxx".

你可能感兴趣的:(Ajax)