[英]ASP.NET: How to get JavaScript variable from popup window?
This is code for Firefox. 这是Firefox的代码。
Default.aspx: Default.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>
Default.aspx.cs: Default.aspx.cs:
protected void Page_Load(object sender, EventArgs e)
{
TextBox1.Text = "Hello";
Button1.Attributes.Add("onClick", "javascript:InvokePop('" + TextBox1.ClientID + "');");
}
PopupWin.aspx: 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: 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();
}
In this case I click button on Default.aspx page and open Popup.aspx as popup window. 在这种情况下,我单击Default.aspx页面上的按钮并打开Popup.aspx作为弹出窗口。 I enter some value to text box in Popup.aspx and press button.
我在Popup.aspx的文本框中输入一些值,然后按按钮。 The new value appears in second text box on Default.aspx page.
新值出现在Default.aspx页上的第二个文本框中。
That works, but how can I pass the new value entered in Popup.aspx page to some handler in Default.aspx page and use it further? 那行得通,但是如何将Popup.aspx页中输入的新值传递给Default.aspx页中的某些处理程序,并进一步使用呢? For example, I can have another ASPX button on Default.aspx page and when I click it I can use the value entered in Popup.aspx page.
例如,我可以在Default.aspx页上有另一个ASPX按钮,单击它时,我可以使用在Popup.aspx页中输入的值。
Well what you can do is the following: 那么,您可以执行以下操作:
Add a hiddenField on the first page. 在第一页上添加一个hiddenField。 Im calling it "hfPopupValue".
我称它为“ hfPopupValue”。
In pop.js u are currently doing this: 在pop.js中,您当前正在执行此操作:
window.opener.document.getElementById("TextBox2").value = newValue;
You can change this to: 您可以将其更改为:
window.opener.document.getElementById("hfPopupValue").value = newValue;
Afterwards you can just read the value from the hiddenField. 之后,您只需从hiddenField读取值即可。 This should solve your problem.
这应该可以解决您的问题。
Have you thought of using jQueryUI's Dialog , which lets you keep all controls on the same page, meaning cleaner code. 您是否考虑过使用jQueryUI的Dialog ,它使您可以将所有控件都放在同一页面上,这意味着代码更简洁 。
It also doesn't look as nasty as a JavaScript popup window. 它也不像JavaScript弹出窗口那样令人讨厌。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.