ASP.NET两个页面传递值的实现

本文介绍如何使用 ASP.NET 中的 window.showModalDialog 和 window.open 方法实现子窗口向父窗口传递值。通过创建三个页面 TestTransferValue.aspx、TestShowModalDialog.aspx 和 TestOpen.aspx,演示具体的 HTML 和 JavaScript 实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

创建三个aspx文件:TestTransferValue.aspx(父页面)、TestShowModalDialog.aspx(测试window.showModalDialog()方法传递值的子页面)和TestOpen.aspx(测试window.open()方法传递值的子页面)。
  下面是三个页面的aspx文件的HTML脚本,至于相应的.aspx.cs文件内容可以不做任何改动。
  这是TestOpen.aspx文件的HTML脚本:


<%@ Page language="c#" Codebehind="TestOpen.aspx.cs" AutoEventWireup="false" Inherits="Camus.TestOpen" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
        <HEAD>
                <title>Test Open</title >
                <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
                <meta name="CODE_LANGUAGE" Content="C#">
                <meta name="vs_defaultClientScript" content="JavaScript">
                <meta name="vs_targetSchema" content=http://schemas.microsoft.com/intellisense/ie5>
                <script>
                function CreateReturnValue(closeWindow)
                {
                        var txtReturnValue=document.getElementById("txtReturnValue");
                        if (txtReturnValue.value=="")
                        {
                                window.alert("请输入传递值");
                                return ;
                        }
                        var txtOpenerReturnValue=window.opener.document.getElementById("txtReturnValue");
                        txtOpenerReturnValue.value=txtReturnValue.value;
                        window.alert("值已经传给父窗体");
                        if (closeWindow)
                        {
                                window.opener=null;
                                window.close();
                        }
                }
                </script>
        </HEAD>
        <body>
                <form id="frmTestOpen" method="post" runat = "server">
                                返回值:<INPUT type="text" id="txtReturnValue" name = "txtReturnValue">
                                <INPUT type="button" value="不关闭页面并返回输入的值" onclick = "CreateReturnValue(false);"><br>
                               <INPUT type="button" value="关闭页面并返回输入的值" onclick = "CreateReturnValue(true);">
                </form>
        </body>
</HTML>


  这是TestShowModalDialog.aspx文件的HTML脚本:

<%@ Page language="c#" Codebehind="TestShowModalDialog.aspx.cs" AutoEventWireup="false" Inherits="Camus.TestShowModalDialog" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
        <HEAD>
                <title>Test ShowModalDialog</title >
                <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
                <meta name="CODE_LANGUAGE" Content="C#">
                <meta name="vs_defaultClientScript" content="JavaScript">
                <meta name="vs_targetSchema" content=http://schemas.microsoft.com/intellisense/ie5>
                <script>
                function CreateReturnValue(closeWindow)
                {
                        var txtReturnValue=document.getElementById("txtReturnValue");
                        if (txtReturnValue.value=="")
                        {
                                window.alert("请输入返回值");
                                return ;
                        }
                        returnValue=txtReturnValue.value;
                        window.close();
                }
                </script>
        </HEAD>
        <body>
                <form id="frmTestShowModalDialog" method="post" runat = "server">
                                返回值:<INPUT type="text" id="txtReturnValue" name = "txtReturnValue"> 
                               <INPUT type="button" value="关闭页面并返回输入的值" onclick = "CreateReturnValue();">
                </form>
        </body>
</HTML>


  这是TestTransferValue.aspx文件的HTML脚本:

<%@ Page language="c#" Codebehind="TestTransferValue.aspx.cs" AutoEventWireup="false" Inherits="Camus.TestTransferValue" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
        <HEAD>
                <title>Test TransferValue</title >
                <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
                <meta name="CODE_LANGUAGE" Content="C#">
                <meta name="vs_defaultClientScript" content="JavaScript">
                <meta name="vs_targetSchema" content=http://schemas.microsoft.com/intellisense/ie5>
                <script>
                function TestShowModalDialog()
                {
                        var rv=window.showModalDialog("TestShowModalDialog.aspx",window,"dialogWidth=400px;dialogHeight= 300px");
                        var txtReturnValue=document.getElementById("txtReturnValue");
                        if (rv==undefined)
                        {
                                txtReturnValue.value="没有返回值";
                        }
                        else
                        {
                                txtReturnValue.value=rv;
                        }
                }
                function TestOpen()
                {
                        window.open("TestOpen.aspx","_blank","height=300,width=400");
                }
                </script>
        </HEAD>
        <body>
                <form id="frmTestTransferValue" method="post" runat = "server">
                               <INPUT type="text" id="txtReturnValue" name = "txtReturnValue"><br>
                               <INPUT type="button" value="用window.showModalDialog()方法" onclick = "TestShowModalDialog();"><br>
                               <INPUT type="button" value="用window.open()方法" onclick = "TestOpen();"><br>

                </form>
        </body>
</HTML>


  启动页面设置成TestTransfer.aspx。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值