【问题标题】:ASP.NET: How to get JavaScript variable from popup window?ASP.NET:如何从弹出窗口中获取 JavaScript 变量?
【发布时间】:2013-02-11 20:48:53
【问题描述】:

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

【问题讨论】:

    标签: javascript asp.net return-value


    【解决方案1】:

    那么你可以做的是:

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

    在 pop.js 中你目前正在这样做:

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

    您可以将其更改为:

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

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

    【讨论】:

      【解决方案2】:

      您是否想过使用jQueryUI's Dialog,它可以让您将所有控件保持在同一页面上,这意味着代码更简洁。

      它看起来也不像 JavaScript 弹出窗口那么讨厌。

      【讨论】:

      • 你说得对,但我现在只需要解决这个问题。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-29
      相关资源
      最近更新 更多