2013-02-11 29 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