SharePoint 集成Ajax 的应用
在SharePoint中集成Ajax的功能并不复杂,只需要8个步骤即可。第一个是安装Ajax的支持,剩下七个则是修改需要提供Ajax支持的网站的web.config文件即可。
1.安装ASP.NET AJAX Extensions 1.0。也可以用3.5的dll.不过<add assembly="System.Web.Extensions, Version=1.0.61025.0,Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>要根据assembly里的最新的System.Web.Extensions。我用
<SafeControl Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" Namespace="System.Web.UI" TypeName="*" Safe="True" />。可行。
2.打开SharePoint网站集所在的WebApplication的目录。例如:C:"inetpub"wwwroot"wss"virtualdirectories"80。并打开目录中的web.config文件。这里我推荐使用VS来编辑这个文件,理由是不容易出错,如果设置有错误则会有红色下划线的职能提示,而且有自动缩进,保证格式。在记事本里面却不能保证这些。
3.在<configSections>标记中添加如下的<sectionGroup>元素。
<sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"> |
4.在<system.web>/<pages>标记中添加<controls>部分。
<controls> |
5.在<compilation>标记的<assemblies>元素中添加如下标记。
<add assembly="System.Web.Extensions, Version=1.0.61025.0,Culture=neutral, PublicKeyToken=31bf3856ad364e35"/> |
6.在<httpHandlers>部分的最后注册HTTP Handlers。
<add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/> |
7.在<httpModules>部分中所有module之后添加注册下面的HTTP Module。
<add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/> |
8.在<SharePoint>/<SafeControls>中为System.Web.Extensions组件添加SafeControl。
<SafeControl Assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" Namespace="System.Web.UI" TypeName="*" Safe="True" /> |
9.最后,在<configuration>标记内的最低部(仅在</configuration>之前),添加如下内容。
<system.web.extensions> |
完成。
using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
namespace WebPartForAjax
{
[Guid("167a7058-cd16-4229-84ea-4da3bcb67858")]
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);
}
}
}