父表單js代碼(開啟子表單):
js_open 1 <script type="text/javascript">
2 //開啟強制回應對話方塊 取得傳回值
3 function OpenModalDialog() {
4 //用一個value來接受傳回值
5 var value = window.showModalDialog("ModalDialogChild.aspx", "ModalDialog", "dialogWidth=800px; dialogHeight=1000px; center:Yes; Help:No; Resizable:No; Status:no; edge:sunken");
6 $("#txtMsg").val(value);
7 }
8
9 //通過window.open開啟表單 取得傳回值
10 function OpenWin() {
11 window.open("winChild.aspx", "window", "height=800,width=1000,top=0,left=0,toolbar=no,menubar=no,scrollbars=no,location=no,status=no");
12 }
13 </script>
父表單頁面html:
html 1 <body>
2 <form id="form1" runat="server">
3 <div>
4 <input type="button" id="btn" value="OpenModalDialog" onclick="OpenModalDialog();" />
5 <br />
6 <br />
7 ModalDialog:<asp:TextBox ID="txtMsg" runat="server"></asp:TextBox>
8 <br />
9 <br />
10 <input type="button" id="Button1" value="OpenWin" onclick="OpenWin();" />
11 <br />
12 <br />
13 OpenWin:<asp:TextBox ID="txtWinMsg" runat="server"></asp:TextBox>
14 </div>
15 </form>
16 </body>子表單_強制回應對話方塊:ModalDialog 1 <head runat="server">
2 <title></title>
3 <script type="text/javascript">
4 function GetValue() {
5 //通過window.returnValue來傳回值到父表單 由父表單中定義的value接收
6 window.returnValue = document.getElementById("txtMsg").value;
7 window.close();
8 }
9 </script>
10 </head>
11 <body>
12 <form id="form1" runat="server">
13 <div>
14 <asp:TextBox ID="txtMsg" runat="server" onblur="GetValue();"></asp:TextBox>
15 </div>
16 </form>
17 </body>子表單_Window.Open頁面:window.open 1 <head runat="server">
2 <title></title>
3 <script type="text/javascript">
4 function SetWinValue() {
5 //window.opener為開啟該頁面的父頁面引用 可以用過引用來擷取父表單的控制項
6 window.opener.document.getElementById("txtWinMsg").value = document.getElementById("txtMsg").value;
7 //下面語句為了防止關閉頁面時 出現關閉提示
8 window.opener = null;
9 window.open('', '_self', '');
10 //關閉
11 window.close();
12 }
13 </script>
14 </head>
15 <body>
16 <form id="form1" runat="server">
17 <div>
18 <input type="text" id="txtMsg" />
19 <input type="button" id="btn" value="給父表單賦值" onclick="SetWinValue();" />
20 </div>
21 </form>
22 </body>