internal sealed class PageRequestManager { // ... internal void OnInit() { if (_owner.EnablePartialRendering && !_owner._supportsPartialRenderingSetByUser) { IHttpBrowserCapabilities browser = _owner.IPage.Request.Browser; bool supportsPartialRendering = (browser.W3CDomVersion >= MinimumW3CDomVersion) && (browser.EcmaScriptVersion >= MinimumEcmaScriptVersion) && browser.SupportsCallback; if (supportsPartialRendering) { supportsPartialRendering = !EnableLegacyRendering; } _owner.SupportsPartialRendering = supportsPartialRendering; } if (_owner.IsInAsyncPostBack) { _owner.IPage.Error += OnPageError; } } // ... }
internal static class AjaxFileUploadUtility { internal static bool IsInIFrameAsyncPostBack(NameValueCollection requestBody) { string[] values = requestBody.GetValues("__AjaxFileUploading__"); if (values == null) return false; foreach (string value in values) { if (value == "__IsInAjaxFileUploading__") { return true; } } return false; } // ... } [PersistChildren(false)] [ParseChildren(true)] [NonVisualControl] public class AjaxFileUploadHelper : Control { // ScriptManager members; private static FieldInfo isInAsyncPostBackFieldInfo; private static PropertyInfo pageRequestManagerPropertyInfo; // PageRequestManager members; private static MethodInfo onPageErrorMethodInfo; private static MethodInfo renderPageCallbackMethodInfo; static AjaxFileUploadHelper() { Type scriptManagerType = typeof(ScriptManager); isInAsyncPostBackFieldInfo = scriptManagerType.GetField( "_isInAsyncPostBack", BindingFlags.Instance | BindingFlags.NonPublic); pageRequestManagerPropertyInfo = scriptManagerType.GetProperty( "PageRequestManager", BindingFlags.Instance | BindingFlags.NonPublic); Assembly assembly = scriptManagerType.Assembly; Type pageRequestManagerType = assembly.GetType("System.Web.UI.PageRequestManager"); onPageErrorMethodInfo = pageRequestManagerType.GetMethod( "OnPageError", BindingFlags.Instance | BindingFlags.NonPublic); renderPageCallbackMethodInfo = pageRequestManagerType.GetMethod( "RenderPageCallback", BindingFlags.Instance | BindingFlags.NonPublic); } public static AjaxFileUploadHelper GetCurrent(Page page) { return page.Items[typeof(AjaxFileUploadHelper)] as AjaxFileUploadHelper; } private bool isInAjaxUploading = false; protected override void OnInit(EventArgs e) { base.OnInit(e); if (this.Page.Items.Contains(typeof(AjaxFileUploadHelper))) { throw new InvalidOperationException("One AjaxFileUploadHelper per page."); } this.Page.Items[typeof(AjaxFileUploadHelper)] = this; this.EnsureIsInAjaxFileUploading(); } private void EnsureIsInAjaxFileUploading() { this.isInAjaxUploading =
AjaxFileUploadUtility.IsInIFrameAsyncPostBack(this.Page.Request.Params); if (this.isInAjaxUploading) { isInAsyncPostBackFieldInfo.SetValue( ScriptManager.GetCurrent(this.Page), true); this.Page.Error += new EventHandler(Page_Error); } } private void Page_Error(object sender, EventArgs e) { // ... } private object _PageRequestManager; private object PageRequestManager { get { if (this._PageRequestManager == null) { this._PageRequestManager = pageRequestManagerPropertyInfo.GetValue( ScriptManager.GetCurrent(this.Page), null); } return this._PageRequestManager; } } // ... }
public bool SupportAjaxUpload { get { return _SupportAjaxUpload; } set { _SupportAjaxUpload = value; } } protected override void OnPreRender(EventArgs e) { base.OnPreRender(e); if (this.isInAjaxUploading) { this.Page.SetRenderMethodDelegate(new RenderMethod(this.RenderPageCallback)); } if (this.Page.IsPostBack || !this.SupportAjaxUpload) return; if (!ScriptManager.GetCurrent(this.Page).IsInAsyncPostBack) { ScriptReference script = new ScriptReference( "Jeffz.Web.AjaxFileUploadHelper.js", this.GetType().Assembly.FullName); ScriptManager.GetCurrent(this.Page).Scripts.Add(script); } }
AjaxFileUploadHelper.GetCurrent(this.Page).SupportAjaxUpload = false;
if (this.isInAjaxUploading) { this.Page.SetRenderMethodDelegate(new RenderMethod(this.RenderPageCallback)); }
private void RenderPageCallback(HtmlTextWriter writer, Control pageControl) { AjaxFileUploadUtility.WriteScriptBlock(this.Page.Response, true); StringBuilder sb = new StringBuilder(); HtmlTextWriter innerWriter = new HtmlTextWriter(new StringWriter(sb)); renderPageCallbackMethodInfo.Invoke(
this.PageRequestManager, new object[] { innerWriter, pageControl }); writer.Write(sb.Replace("*/", "*//*").ToString()); AjaxFileUploadUtility.WriteScriptBlock(this.Page.Response, false); } private void Page_Error(object sender, EventArgs e) { AjaxFileUploadUtility.WriteScriptBlock(this.Page.Response, true); onPageErrorMethodInfo.Invoke(this.PageRequestManager, new object[] { sender, e }); AjaxFileUploadUtility.WriteScriptBlock(this.Page.Response, false); }
本文出自 “赵��” 博客,转载请与作者联系!