window.showModalDialog的传值和返回值

window.showModalDialog(URL,dialogArgments.features) 打开一个新窗口


URL为要开启的网页名字。
dialogArgments为设定好传递给新视窗网页的参数,可以为任意数据类型。
feature
与open()的类似,都是格式方面的设定。调用格式为featureName1:featureValue1:(分号)featureName2:featureValue2:


关于feature具体的参数我就不详细写了,看名字就应该知道什么用处了吧。
certer , dialogHeight, dialogLeft,dialogTop,dialogWidth,help(是否显示help按钮,下同),status,resizeable
值=1为yes,0为no.

我认为最重要的是dialogArgments,可以传递值到新的窗口。
第二重要就是 它的返回值
window.returnValue.可以在showModalDialog开启的窗口关闭后前,回传一个任意类型的值。

dialogArgments 
可以传入一个变量,但是 我认为最好是传入一个window 这样的话,不光你可以调用你前面定义的变量names 和a
等等,还可以取到前一个页面上任何元素的值.如下面的代码所示:

<script language =javascript>
    alert(window.dialogArguments.names);
    alert(window.dialogArguments.a );
   // alert(window.dialogArguments.document.form1.t1.value)
    window.dialogArguments.a = "Hello World";  //可以改变WebFromA里面的变量的值
    window.dialogArguments.document.form1.t1.value  = "t1";//可以修改WebFrom1里面的TextBox的value;

在WebFromB.aspx页面 我们可以取到 names 和a
的值还可以取到WebFromA的值.,还可以给它赋值.
整体的代码如下:
WebFromA.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebFormA.aspx.cs" Inherits="_5demo.ShowDig.WebFormA" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
    <script language=javascript>
    function fnA()
    {
        names = new Array(3);
        names[0] = "chenzhifeng";
        names[1] = "chenjiang";
        
        a= "shuhui";
     temp=   window.showModalDialog("WebFormB.aspx",window);
       // alert(a);
       document.getElementById("t3").value = temp;
    }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <input type=text value ="feng" id = "t1" />
    <br />
    
    <input type=text value ="chen" id = "t2" />
    <input type=button value = "Click Me" onclick ="fnA();" id = "btn1" />
    <p>
    返回值:<input type = "text" id= "t3" />
    </div>
    </form>
</body>
</html>

WebFormB.aspx 如下:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebFormB.aspx.cs" Inherits="_5demo.ShowDig.WebFormB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
    <script language =javascript>
   // alert(window.dialogArguments.names);
    alert(window.dialogArguments.a );
   // alert(window.dialogArguments.document.form1.t1.value)
    window.dialogArguments.a = "Hello World";  //可以改变WebFromA里面的变量的值
    window.dialogArguments.document.form1.t1.value  = "t1";//可以修改WebFrom1里面的TextBox的value;
   
   
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <input type=text id = "tt" value = "Hello ZiFeng"/>
    <script language =javascript>
    window.returnValue = document.getElementById("tt").value;
    window.close();
    </script>
    </div>
    </form>
</body>
</html>

在WebFormB.aspx中 window.returnValue是用于返回给WebFromA.aspx的值.
在WebFromA.aspx中,可以用一个变量来接收这个值 temp=window.showModalDialog("WebFormB.aspx",window);

posted on 2013-01-16 18:56  hukailee  阅读(246)  评论(0编辑  收藏  举报