背景
我们经常能遇到打开一个弹出页面以后,当关闭模态弹出页面后,需要刷新父页面的情况。
页面POSTBACK逻辑分析
下面是一段打开模态窗口的代码
var OpenWindow;
function OpenModalDialogWindow()
{
OpenWindow = window.showModalDialog("b.html",'newwindow',"dialogHeight:100px,center:yes,resizable:no,status:no");
}
接下来了解下ASP.NET是如何通过页面POST Back来触发control的事件的,来通过后台处理逻辑的
<form name="aspnetForm" method="post" action="MSAjax.aspx" id="aspnetForm">
<div>
<input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" />
<input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" />
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKLTczODA5OTY2MGRk9pWvSuck0jPaq47w0nYaFEmYOrg=" />
</div>
<script type="text/javascript">
//<![CDATA[
var theForm = document.forms['aspnetForm'];
if (!theForm) {
theForm = document.aspnetForm;
}
function __doPostBack(eventTarget, eventArgument) {
if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
theForm.__EVENTTARGET.value = eventTarget;
theForm.__EVENTARGUMENT.value = eventArgument;
theForm.submit();
}
}
首先是三个隐藏域中:
__EVENTTARGET代表触发Event的Control的Unique name;
__EVENTARGUMENT代表为Event Handler定义的额外的参数;
__VIEWSTATE:代表的是Viewstate的内容
其次是方法__doPostBack,附上隐藏域的值后,提交FORM到Server端
Button实现了接口IPostBackEventHandler,而接口中的方法是void RaisePostBackEvent(string eventArgument)
如果该Control确实实现了该Interface,那么调用Page的RaisePostBackEvent方法,这是一个Virtual的方法,可以被Override
[EditorBrowsable(EditorBrowsableState.Advanced)]
protected virtual void RaisePostBackEvent(IPostBackEventHandler sourceControl, string eventArgument)
{
sourceControl.RaisePostBackEvent(eventArgument);
}
protected virtual void RaisePostBackEvent(string eventArgument)
{
base.ValidateEvent(this.UniqueID, eventArgument);
if (this.CausesValidation)
{
this.Page.Validate(this.ValidationGroup);
}
this.OnClick(EventArgs.Empty);
this.OnCommand(new CommandEventArgs(this.CommandName, this.CommandArgument));
}
RaisePostBackEvent方法直接调用该sourceControl的RaisePostBackEvent,并传入一个eventArgument参数。
sourceControl的RaisePostBackEvent方法先进行Validation,然后先后出发两个Event:OnClick 和OnCommand
Server接受到Client的Postback,对于事件的Web Control(或者Html Server Control),如果实现了System.Web.UI.IPostBackEventHandler接口,
会调用Page的virtual方法:RaisePostbackEvent,我们现在来Override这个方法:
protected override void RaisePostBackEvent(IPostBackEventHandler sourceControl, string eventArgument)
{
sourceControl = this.btnAsynClick;
base.RaisePostBackEvent(sourceControl, eventArgument);
}
结果很明显了。不管点什么BUTTON,结果都触发了“btnAsynClick”这个按钮。试验结束以后将这行代码注释掉。
关闭Dialog后模拟POSTBACK回刷
在了解服务器端如何POST Back在后台处理页面逻辑后。关闭模态窗口后的刷新也变的简单起来。
protected string RegisterControlPostBackEvent(string augument)
{
string script = ClientScript.GetPostBackEventReference(Button1, augument);
return script;
}
上面这个函数就是用来模拟button触发postback,类似于javascript中的
__doPostBack('ctl00$ContentPlaceHolder1$Button1','Test1')
在客户端调用后台函数
function openModelDialog(url, iWidth, iHeight, actionType) {
var iTop = (window.screen.height - iHeight) / 2;
var iLeft = (window.screen.width - iWidth) / 2;
var result = window.showModalDialog(
url, window, 'dialogHeight: ' + iHeight + 'px;dialogWidth: ' + iWidth + 'px;dialogTop: ' + iTop + '; dialogLeft: ' + iLeft + '; resizable: no; status: no;scroll:yes');
if(result != null && result){
switch (actionType) {
case "Test1": <%=RegisterControlPostBackEvent("Test1")%>;break;
}
}
}
最后我们要在弹出页面中定义一个Button用来关闭弹出页面
<input type="button" id="btnClose" value="close" οnclick="javascript:window.returnValue='Test1';window.close();" />
当点击这个button,关闭Dialog后,就会触发父页面的PostBack了
不过由于postback不是由服务器控件本身触发,所以服务器控件和提交的数据本身是不匹配的,会过不了Page.Validate()方法。
有2种方案:
1:<%@ Page Title="" Language="C#" MasterPageFile="~/Shared/Site.Master" AutoEventWireup="true" CodeBehind="OpenDialog.aspx.cs" Inherits="MyBestPractice.Content.OpenDialog" EnableEventValidation="false" %>页面头加上红色这段
2: 推荐方法:
protected override void Render(HtmlTextWriter writer)
{
ClientScript.RegisterForEventValidation(Button1.UniqueID, "Test1");
base.Render(writer);
}
重写Page.Render函数,由于我们回调是模拟Button1的事件,传输的数据是“Test1”,所以要对控件和数据进行注册。
这样就完全全部内容了。