让UpdatePanel支持文件上传(2):服务器端组件
上面这段代码会在ScriptManager的OnInit方法中被调用。请注意红色部分的代码,“_owner”变量是当前页面上的ScriptManager。在页面受到一个真正的异步会送之后,PageRequestManager会响应页面的Error事件,并且将错误信息用它定义的格式输出。如果我们只是修改了ScriptManager的私有field,那么如果在异步回送时出现了一个未捕获的异常,那么页面就会输出客户端未知的内容,导致在客户端解析失败。所以我们必须保证这种情况下的输出和真正的异步回送是相同的,所以我们就可以使用以下的做法来解决错误处理的问题。
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;
}
}
![]()
// ...
}
0
相关文章
