考慮下列案例:
- 您使用 Microsoft AJAX 1.0 控制工具組,如 Microsoft ASP.NET 或 AJAX 1.0 擴充程式的 ASP.NET 建立自訂網頁組件的 Microsoft Office SharePoint Server 2007 網站或 Microsoft Windows SharePoint 服務 3.0 的網站。
- 自訂網頁組件包含 UpdatePanel 控制項,其中包括如 LinkButton 控制項的 _doPostBack() 函數。
- 到 SharePoint 服務 2007年網站或為 Windows SharePoint 服務 3.0 的網站,您可以新增自訂的網頁組件。Windows SharePoint 服務 3.0 網站的 URL 或 SharePoint Server 2007 Web 網站包含國際字元或特定語言的字元。
- 您造訪 Windows SharePoint 服務 3.0 網站或 SharePoint Server 2007 Web 站台。然後,您嘗試更新 [網頁組件] 中選取的內容。
在這種情況下一般回傳要求發生而不是非同步 ASP.NET AJAX 回傳要求。
之所以發生這個問題,是因為 Windows SharePoint 服務與 ASP.NET AJAX 快取某些類型的表單的動作。因此,可能會發生衝突。
若要解決這個問題,使用類似下列範例將
UpdatePanel 控制項新增至網頁組件在 SharePoint Server 2007 或 Windows SharePoint 服務 3.0 中的程式碼。
private void EnsureUpdatePanelFixups()
{
if (this.Page.Form != null)
{
String fixupScript = @"
_spBodyOnLoadFunctionNames.push(""_initFormActionAjax"");
function _initFormActionAjax()
{
if (_spEscapedFormAction == document.forms[0].action)
{
document.forms[0]._initialAction = document.forms[0].action;
}
}
var RestoreToOriginalFormActionCore = RestoreToOriginalFormAction;
RestoreToOriginalFormAction = function()
{
if (_spOriginalFormAction != null)
{
RestoreToOriginalFormActionCore();
document.forms[0]._initialAction = document.forms[0].action;
}
} 下列的程式碼是包含先前
UpdatePanel 控制項的網頁組件的範例。 Additionally, the
EnsureUpdatePanelFixups method is used to register the Script Manager control in the Web Part.
public class AjaxUpdatePanelPart : WebPart
{
private Label label;
private TextBox textBox;
protected override void CreateChildControls()
{
base.CreateChildControls();
this.EnsureUpdatePanelFixups();
UpdatePanel up = new UpdatePanel();
up.ID = "UpdatePanel1";
up.ChildrenAsTriggers = true;
up.UpdateMode = UpdatePanelUpdateMode.Conditional;
this.Controls.Add(up);
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);
LinkButton button = new LinkButton();
button.Text = "Say Hello";
button.ID = "HelloButton"; // Some controls need an ID to make them work with AJAX postbacks.
button.Click += new EventHandler(HandleButtonClick);
up.ContentTemplateContainer.Controls.Add(button);
}
private void HandleButtonClick(object sender, EventArgs eventArgs)
{
this.label.Text = "Hello " + this.textBox.Text;
}
private void EnsureUpdatePanelFixups()
{
if (this.Page.Form != null)
{
String fixupScript = @"
_spBodyOnLoadFunctionNames.push(""_initFormActionAjax"");
function _initFormActionAjax()
{
if (_spEscapedFormAction == document.forms[0].action)
{
document.forms[0]._initialAction = document.forms[0].action;
}
}
var RestoreToOriginalFormActionCore = RestoreToOriginalFormAction;
RestoreToOriginalFormAction = function()
{
if (_spOriginalFormAction != null)
{
RestoreToOriginalFormActionCore();
document.forms[0]._initialAction = document.forms[0].action;
}
}
";
ScriptManager.RegisterStartupScript(this, typeof(AjaxUpdatePanelPart), "UpdatePanelFixup", fixupScript, true);
}
}
}