gpt4 book ai didi

javascript - ASP.NET:如何从弹出窗口获取 JavaScript 变量?

转载 作者:行者123 更新时间:2023-11-28 02:26:50 24 4
gpt4 key购买 nike

这是 Firefox 的代码。

默认.aspx:

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
<script type="text/javascript" src="pop.js"></script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input id="Hidden1" type="hidden" runat="server" />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Popup" />
</div>
</form>
</body>
</html>

默认.aspx.cs:

    protected void Page_Load(object sender, EventArgs e)
{
TextBox1.Text = "Hello";
Button1.Attributes.Add("onClick", "javascript:InvokePop('" + TextBox1.ClientID + "');");
}

PopupWin.aspx:

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
<script type="text/javascript" src="pop.js"></script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txtValue" runat="server"></asp:TextBox><br />
<br />
<input type="button" id="btnReturnValue" value="Return value back" onclick="ReturnValue();" />
</div>
</form>
</body>
</html>

pop.js:

function InvokePop(fname)
{
val = document.getElementById(fname).value;
retVal = window.open("PopupWin.aspx?Control1=" + fname, 'Show Popup Window', 'height=90,width=250,resizable=yes,modal=yes');
retVal.focus();
}

function ReturnValue()
{
var newValue = document.getElementById("txtValue").value;
if ((window.opener != null) && (!window.opener.closed))
{
window.opener.document.getElementById("TextBox2").value = newValue;
}
window.close();
}

在本例中,我单击 Default.aspx 页面上的按钮并打开 Popup.aspx 作为弹出窗口。我在 Popup.aspx 中的文本框中输入一些值,然后按按钮。新值将显示在 Default.aspx 页面上的第二个文本框中。

这可行,但是如何将 Popup.aspx 页面中输入的新值传递给 Default.aspx 页面中的某个处理程序并进一步使用它?例如,我可以在 Default.aspx 页面上有另一个 ASPX 按钮,当我单击它时,我可以使用在 Popup.aspx 页面中输入的值。

最佳答案

那么您可以执行以下操作:

在首页添加一个hiddenField。我称之为“hfPopupValue”。

在 pop.js 中,你当前正在执行以下操作:

window.opener.document.getElementById("TextBox2").value = newValue;

您可以将其更改为:

window.opener.document.getElementById("hfPopupValue").value = newValue;

之后你就可以从hiddenField中读取值了。这应该可以解决您的问题。

关于javascript - ASP.NET:如何从弹出窗口获取 JavaScript 变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14811826/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com