這是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頁面中輸入的值。
你說得對,但我現在需要解決這個問題。 – tesicg 2013-02-12 04:10:50