1

これは 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>

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 ページの 2 番目のテキスト ボックスに新しい値が表示されます。

それは機能しますが、Popup.aspx ページに入力された新しい値を Default.aspx ページのハンドラーに渡し、それをさらに使用するにはどうすればよいですか? たとえば、Default.aspx ページに別の ASPX ボタンを配置し、クリックすると Popup.aspx ページに入力された値を使用できます。

4

2 に答える 2

1

さて、あなたができることは次のとおりです。

最初のページに hiddenField を追加します。私はそれを「hfPopupValue」と呼んでいます。

pop.jsでは、現在これを行っています:

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

これを次のように変更できます。

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

その後、hiddenField から値を読み取ることができます。これで問題が解決するはずです。

于 2013-02-11T12:48:53.370 に答える
0

jQueryUI の Dialogを使用することを考えたことはありますか?これにより、すべてのコントロールを同じページに保持できます。つまり、コードがよりクリーンになります。

また、JavaScript のポップアップ ウィンドウほど見栄えもよくありません。

于 2013-02-11T12:44:46.530 に答える