在 ASP.NET 1.1 中,在模态页面和调用页面之间传递参数






3.47/5 (6投票s)
2005年8月20日
3分钟阅读

52673

568
在 ASP.NET 1.1 中,在对话框页面和调用页面之间传递参数
引言
在 ASP.NET 1.1 中,在对话框页面和调用页面之间传递参数。我写了一个控件来完成这项任务,详见 "ASP.NET 1.1 中的 ModalDialogHelper 控件"
有时,我们需要调用一个方法以模态方式显示对话框,将一些参数传递给它,并在它关闭时从它那里获取返回值。
在 asp 时代,使用 javascript 在客户端完成这项任务,并将包含此值的表单提交给后台程序。现在,asp.net 是 b/s 架构的主要工具。我想将这种方式更改为 asp.net 1.1。
在 asp.net 中,我们使用事件模式来快速编程。事件模式的本质是提交表单。当页面在客户端呈现时,所有事件设置都将转换为 javascript 以提交表单。通常,我们可以在客户端 html 中看到下面的代码段
<input type="hidden" name="__EVENTTARGET" value="" />
<input type="hidden" name="__EVENTARGUMENT" value="" />
<input type="hidden" name="__VIEWSTATE" value="dDw4MTc2ODI0Mzc7Oz4Sp8vtufeHPnCSpIk8Obsuj84lVg==" />
<script language="javascript" type="text/javascript">
<!--
function __doPostBack(eventTarget, eventArgument) {
var theform;
if (window.navigator.appName.toLowerCase().indexOf("microsoft") > -1) {
theform = document.Form1;
}
else {
theform = document.forms["Form1"];
}
theform.__EVENTTARGET.value = eventTarget.split("$").join(":");
theform.__EVENTARGUMENT.value = eventArgument;
theform.submit();
}
// -->
</script>
函数 “__doPostBack” 只是将事件提交给服务器的函数。它使用两个隐藏域来记录事件源和参数。当 "__EVENTTARGET" 填充为 “Button2” 并提交到服务器时,它可以引发服务器控件 “Button2” 的事件。我使用此函数来引发包含从模态对话框接收返回值的代码的事件。请看下面的代码
private void Button1_Click(object sender, System.EventArgs e) { string strScript = "<script defer>\n"+ " var strReturn = window.showModalDialog('list.aspx','desc','dialogWidth:423px;dialogHeight=355px');\n"+ " __doPostBack('Button2',strReturn);"+ "</script>"; if(!this.IsClientScriptBlockRegistered("OpenDialogScript")) this.RegisterStartupScript("OpenDialog",strScript); }
在调用页面中,我添加了一个 LinkButton,其 id 为 “Button2”。顺便说一句,您最好不要使用 Button 或 ImageButton 来生成事件,因为它无法在客户端生成 “__doPostBack” 函数。
在 list.aspx 的服务器代码中,当第一次 Page_Load 时,它应该获取客户端中的 window.dialogArguments,并且当它不是 null 值时,必须提交到服务器。如下所示
private void Page_Load(object sender, System.EventArgs e) { if(!IsPostBack) { string strScript = "<script defer> if(window.dialogArguments!=null) __doPostBack('',window.dialogArguments); </script>"; if(!this.IsClientScriptBlockRegistered("GetArgumentScript")) this.RegisterStartupScript("GetArgumentScript",strScript); } else { if(ViewState["ORDERBY"] == null) { ViewState["ORDERBY"] = Request["__EVENTARGUMENT"]; BindData(); } } }
运行屏幕
然后您可以轻松地从调用页面中的模态页面接收返回值
private void Button2_Click(object sender, System.EventArgs e) { TextBox1.Text = Request["__EVENTARGUMENT"]; }
实际上,有一种通用方法可以获取该值。在调用页面 (WebForm1.aspx) 中,您直接使用 form1.submit 方法。
private void Button1_Click(object sender, System.EventArgs e) { string strScript = "<script defer>\n"+ " var strReturn = window.showModalDialog('list.aspx','desc','dialogWidth:423px;dialogHeight=355px');\n"+ " Form1.action='WebForm1.aspx?src=MODAL&value='+strReturn; Form1.submit();"+ "</script>"; if(!this.IsClientScriptBlockRegistered("OpenDialogScript")) this.RegisterStartupScript("OpenDialog",strScript); }
因此,在 Page_Load 事件中,您应该像下面这样过滤此请求
private void Page_Load(object sender, System.EventArgs e) { if(Request["src"] != null && Request["src"].toUpper()=="MODAL") { string strReturnValue = Request["value"]; // code to do with the return value goes here... } }
最后一点,如果您在模态页面中提交表单,它将打开一个新页面来处理该请求。为了解决这种情况,您可以将属性 smartNavigation="True" 设置为 True。此选项将在 iframe 中提交表单。另一种方法,您可以将此代码写在 HTML 文本的 <head> ... </head> 部分中。
<HTML>
<HEAD>
....
<base target=_self>
</HEAD>
....
</HTML>
在 list.aspx 中,我还展示了如何通过 DataGrid 中的复选框选择多行以及一种自定义分页的简便方法。
//modify the connection string below string strConn = "Server=(local);Database=Northwind;Uid=sa;Pwd=xxx";
这是我在这个网站上的第一篇文章,感谢您的阅读,非常抱歉我的英语不好。:p
我的电子邮件是 wangran@baosight-hy.com