3,242
社区成员
发帖
与我相关
我的任务
分享
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;
[WebPartStorage(Storage.None)]
public ScriptManager AjaxManager
{
get { return _AjaxManager; }
set { _AjaxManager = value; }
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
_AjaxManager = ScriptManager.GetCurrent(this.Page);
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);
}
}
}
protected override void OnLoad(EventArgs e)
{
this.EnsureUpdatePanelFixups();
base.OnLoad(e);
}
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);
this.Controls.Add(up);
}
private void TextBoxChanged(object sender, EventArgs eventArgs)
{
this.label.Text = "Hello " + this.textBox.Text;
}
private void HandleButtonClick(object sender, EventArgs eventArgs)
{
this.label.Text = "Hello " + this.textBox.Text;
}
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);
}
}
}