asp.net模态窗口返回值

  个人感觉模态窗口在做网站的时候,使用到的比较少,前段时间在做项目时要实现以模态窗口传值和接收返回值,

模态窗口传值实现比较简单,但是做好后发现在Chrome浏览器中接收不到返回值,修改好Chrome浏览器的问题后

大概过了有两个星期又发现在IE6下会报错,最终在网上搜索解决办法才修改好

代码:

A为父页面,B为子页面

A页面代码:

 //打开选择客户的模态窗口
        function sc(tag) {
            var recdata = false;
            var strDialogFeatures = "status=no;center=yes;help=no;dialogWidth=700px;dialogHeight=600px;scroll=yes;resize=no";
            recdata = showModalDialog("Modal_List_forother.aspx?id=" + tag + "", "newwindow", strDialogFeatures);
       //此处的if判断语句必须需要,如果直接用recdata=window.returnValue赋值的话在IE6下会无法获取到返回值,至于原因,我也不清楚I'M SORRY o(╯□╰)o
if (recdata == undefined) { recdata = window.returnValue; } if (recdata != undefined) { //刷新父窗口 此处可以不刷新 因项目功能需要 所以在此处做了刷新操作 var url = window.location.href.toString();
         //此处省略对变量url的部分操作,根据需要实现的功能不同代码也会不一样 window.location.href
= url; } }

B页面代码:

首先在要想在IE6下也能接收模态窗口返回值 先要在B页面的head部分添加代码<base target="_self" />

如下:

<head runat="server">
    <title>子窗口</title>
    <link href="Styles/basic.css" rel="stylesheet" type="text/css" />
    <base target="_self" />
</head>

要想从后台返回值到父页面去,我们还需在页面的cs文件中添加如下代码:

           string strscripts = "";
           strscripts = strscripts + "<script type="text/javascript">";
       //这是原先的返回值代码,也是正确的,只是嫌太长不想用
//strscripts = strscripts + " if(window.opener!=undefined){ window.opener.returnValue = '" + name + "'; }else{window.returnValue = '" + name //+ "';} window.close();</script>"; strscripts = strscripts + " window.returnValue = '" + name+ "';window.close();</script>"; ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "<script type="text/javascript"> window.returnValue = '" + name + "';window.close();</script>");
原文地址:https://www.cnblogs.com/xuxw/p/3416455.html