(一) 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
* (具體請參考上文中提到的「mossTeam Devblog」) */
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".