javascript实现打开窗体以及窗体间传值
父窗体js代码(打开子窗体):

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>
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:

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>
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>
子窗体_模式对话框:

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>
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页面:

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>
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>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· SQL Server 2025 AI相关能力初探
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库