2013-02-11 67 views
1

这是Firefox的代码。ASP.NET:如何从弹出窗口获取JavaScript变量?

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:

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页面中输入的值。

回答

1

那么你可以做的是:

添加hiddenField第一页上。我称之为“hfPopupValue”。

在pop.jsü目前正在做的:

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

您可以将其更改为:

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

之后,你可以只是读从hiddenField值。 这应该可以解决你的问题。

0

你有没有想过使用jQueryUI's Dialog,它可以让你保持在同一页面上的所有控件,这意味着更干净的代码。

它也不像JavaScript弹出窗口那样令人讨厌。

+0

你说得对,但我现在需要解决这个问题。 – tesicg 2013-02-12 04:10:50