using System; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Security.Permissions; using System.ComponentModel; using System.Text; namespace Bruce.Web.Controls { [AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal)] [AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)] [ToolboxData("<{0}:RequestProcesser ID=\"RequestProcesser\" runat=\"server\"></{0}:RequestProcesser>")] public class RequestProcesser : CompositeControl { /// <summary> /// JavaScript物件名稱 /// </summary> private string ObjectName = "__RequestProcesser"; [Browsable(false)] [PersistenceMode(PersistenceMode.InnerProperty)] public ProcesserAction Action { get; set; } [Browsable(false)] [PersistenceMode(PersistenceMode.InnerProperty)] public ITemplate ContentTemplate { get; set; } protected override void OnPreRender(EventArgs e) { ScriptManager sm = ScriptManager.GetCurrent(this.Page); if (sm == null) { #region OnBeginRequest string script = string.IsNullOrEmpty(this.Action.OnBeginRequest.Text.Trim()) ? "document.getElementById('" + this.ClientID + "').style.display = 'block';" : string.Format(@" if((function(){{ {0} }})() == false){{ return false; }}else{{ document.getElementById('{1}').style.display = 'block'; return true; }}", this.Action.OnBeginRequest.Text.Trim(), this.ClientID); this.Page.ClientScript.RegisterOnSubmitStatement(this.Page.GetType(), "OnBeginRequest", script); #endregion } else { StringBuilder script = new StringBuilder(); script.AppendFormat("var {0} = {{}};{1}", this.ObjectName, Environment.NewLine); #region OnBeginRequest script.AppendFormat(@"{0}.OnBeginRequest = function() {{ if((function(){{ {1} }})() == false){{ return false; }}else{{ document.getElementById('{2}').style.display = 'block'; return true; }} }}; //Sys.WebForms.PageRequestManager.getInstance().add_beginRequest({0}.OnBeginRequest); ", this.ObjectName, this.Action == null || this.Action.OnBeginRequest == null ? string.Empty : this.Action.OnBeginRequest.Text.Trim(), this.ClientID); this.Page.ClientScript.RegisterOnSubmitStatement(this.Page.GetType(), "OnBeginRequest", "return " + ObjectName + ".OnBeginRequest();"); #endregion #region OnEndRequest script.AppendFormat(@"{0}.OnEndRequest = function() {{ document.getElementById('{1}').style.display = 'none'; (function(){{ {2} }})(); }}; Sys.WebForms.PageRequestManager.getInstance().add_endRequest({0}.OnEndRequest); ", this.ObjectName, this.ClientID, this.Action == null || this.Action.OnEndRequest == null ? string.Empty : this.Action.OnEndRequest.Text.Trim()); #endregion this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "Processing", script.ToString(), true); } this.Style["display"] = "none"; base.OnPreRender(e); } protected override void CreateChildControls() { Controls.Clear(); if (ContentTemplate != null) { ContentTemplate.InstantiateIn(this); } } public override void DataBind() { CreateChildControls(); ChildControlsCreated = true; base.DataBind(); } } [DesignTimeVisible(false)] public class ProcesserAction { [PersistenceMode(PersistenceMode.InnerProperty),] public Literal OnBeginRequest { get; set; } [PersistenceMode(PersistenceMode.InnerProperty)] public Literal OnEndRequest { get; set; } } }