<helper:JavaScriptUpdater runat="server" ID="Updater" MethodName="Refresh"
ResolveUpdatePanel="OnResolveUpdatePanel" Enabled="True">
<UpdatePanels>
<helper:UpdatePanel UpdatePanelID="UpdatePanel1" />
...
</UpdatePanels>
</helper:JavaScriptUpdater>
namespace UpdatePanelHelperResolveUpdatePanelEventArgs类
{
public class UpdatePanel
{
private string _UpdatePanelID;
public string UpdatePanelID
{
get { return _UpdatePanelID; }
set { _UpdatePanelID = value; }
}
}
}
namespace UpdatePanelHelper
{
public class ResolveUpdatePanelEventArgs : EventArgs
{
private string _ID = null;
public string ID
{
get { return _ID; }
}
private System.Web.UI.UpdatePanel _UpdatePanel = null;
public System.Web.UI.UpdatePanel UpdatePanel
{
get { return _UpdatePanel; }
set { _UpdatePanel = value; }
}
public ResolveUpdatePanelEventArgs(string id)
{
this._ID = id;
}
}
}
namespace UpdatePanelHelper
{
[PersistChildren(false)]
[ParseChildren(true)]
[NonVisualControl]
public class JavaScriptUpdater : Control
{
private bool initialized = false;
private bool _Enabled = true;
public bool Enabled
{
get
{
return this._Enabled;
}
set
{
if (this.initialized)
{
throw new InvalidOperationException(
"Cannot set the property after initialized.");
}
this._Enabled = value;
}
}
private string _MethodName;
public string MethodName
{
get
{
return this._MethodName;
}
set
{
if (this.initialized)
{
throw new InvalidOperationException(
"Cannot set the property after initialized.");
}
this._MethodName = value;
}
}
public event EventHandler<ResolveUpdatePanelEventArgs> ResolveUpdatePanel;
private List<UpdatePanel> _UpdatePanels = new List<UpdatePanel>();
[PersistenceMode(PersistenceMode.InnerProperty)]
public List<UpdatePanel> UpdatePanels
{
get
{
return _UpdatePanels;
}
}
...
}
}
private string clientButtonId = null;
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
this.Page.InitComplete += new EventHandler(Page_InitComplete);
}
private void Page_InitComplete(object sender, EventArgs e)
{
this.initialized = true;
if (this.Enabled)
{
this.Page.Load += new EventHandler(Page_Load);
this.Page.PreRenderComplete += new EventHandler(Page_PreRenderComplete);
}
}
private void Page_Load(object sender, EventArgs e)
{
LinkButton button = new LinkButton();
button.Text = "Update";
button.ID = this.ID + "Button";
button.Style[HtmlTextWriterStyle.Display] = "none";
button.Click += new EventHandler(OnTrigger);
this.Page.Form.Controls.Add(button);
this.clientButtonId = button.UniqueID;
ScriptManager.GetCurrent(this.Page).RegisterAsyncPostBackControl(button);
}
<a id="UpdaterButton" href="javascript:__doPostBack('UpdaterButton','')">Update</a>
private const string BasicScripts =
@"if (!window.UpdatePanels) window.UpdatePanels = {};
UpdatePanels._createUpdateMethod = function(btnId)
{
return function()
{
__doPostBack(btnId, '');
}
}";
private const string RegisterMethodTemplate =
"\nUpdatePanels['{0}'] = UpdatePanels._createUpdateMethod('{1}');";
private void Page_PreRenderComplete(object sender, EventArgs e)
{
this.Page.ClientScript.RegisterClientScriptBlock(
this.GetType(),
"BasicScripts",
JavaScriptUpdater.BasicScripts,
true);
this.Page.ClientScript.RegisterClientScriptBlock(
this.GetType(),
this.clientButtonId,
String.Format(JavaScriptUpdater.RegisterMethodTemplate,
this.MethodName, this.clientButtonId),
true);
}
private void OnTrigger(object sender, EventArgs e)
{
if (this.Enabled)
{
foreach (UpdatePanel panel in this.UpdatePanels)
{
System.Web.UI.UpdatePanel updatePanel =
this.FindUpdatePanel(panel.UpdatePanelID);
if (updatePanel != null)
{
updatePanel.Update();
}
}
}
}
private System.Web.UI.UpdatePanel FindUpdatePanel(string id)
{
System.Web.UI.UpdatePanel result = null;
if (id != null)
{
result = this.NamingContainer.FindControl(id)
as System.Web.UI.UpdatePanel;
}
if (result == null)
{
ResolveUpdatePanelEventArgs e = new ResolveUpdatePanelEventArgs(id);
this.OnResolveUpdatePanel(e);
result = e.UpdatePanel;
}
return result;
}
private void OnResolveUpdatePanel(ResolveUpdatePanelEventArgs e)
{
if (this.ResolveUpdatePanel != null)
{
this.ResolveUpdatePanel(this, e);
}
}
<%@ Register Assembly="UpdatePanelHelper" Namespace="UpdatePanelHelper" TagPrefix="helper" %>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<%= DateTime.Now.ToString() %>
</ContentTemplate>
</asp:UpdatePanel>
<helper:JavaScriptUpdater runat="server" ID="Updater" MethodName="Refresh">
<UpdatePanels>
<helper:UpdatePanel UpdatePanelID="UpdatePanel1" />
</UpdatePanels>
</helper:JavaScriptUpdater>
<input type="button" onclick="UpdatePanels.Refresh()" value="Refresh" />
<script type="text/javascript">
if (!window.UpdatePanels) window.UpdatePanels = {};
UpdatePanels._createUpdateMethod = function(btnId)
{
return function()
{
__doPostBack(btnId, '');
}
}
UpdatePanels['Refresh'] = UpdatePanels._createUpdateMethod('UpdaterButton');
</script>
...
<a id="UpdaterButton"
href="javascript:__doPostBack('UpdaterButton','')"
style="display:none;">Update</a>
本文出自 “赵��” 博客,转载请与作者联系!