c# - 模式对话框未关闭

标签 c# javascript asp.net web-applications modal-dialog

注册页面.aspx

function btnSearchClick()
{
    if (window.showModalDialog)
    {
        window.showModalDialog(
            "Search.aspx", 
            "Search Patient", 
            "dialogWidth:800px; dialogHeight:400px"
        );
        return false;
    }
}

搜索.aspx

$(document).ready(function ()
{
    $("input[id$='btnAdd']").live('click', function (e) {                   
        hidID.value = $.trim($('table td.csstablelisttdselected:first').text());
        return true;
    });
});

Seach.aspx.cs

protected void btnAdd_Click(object sender, EventArgs e)
{
    Response.Redirect("RegistrationPage.aspx?ID=" + hidID.Value, true);
    Page.ClientScript.RegisterStartupScript(
        this.GetType(), 
        "CloseScript", 
        "window.close()", 
        true
    );
}

RegistrationPage.aspx页面中点击按钮会弹出搜索对话框。
搜索页面中,我在hiddenfield中获取ID并重定向到注册页面
当我单击 btn add 时,对话框未关闭,它重定向到对话框内的注册页面。

请不要给出“使用 jquery 对话框”、“或使用其他对话框控件”之类的答案

最佳答案

这是一个经过测试的示例:

默认.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Test</title>
    <script type="text/javascript">
        function btnSearchClick()
        {
            window.returnValue = undefined;
            var result = window.showModalDialog("Search.aspx", window, "dialogHeight:650px; dialogWidth:900px;");
            if (result == undefined)
                result = window.returnValue;
            if (result != null && result != "undefined")
                alert(result);
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <input type="button" id="btnOpen" onclick="btnSearchClick();" />
        </div>
    </form>
</body>
</html>

搜索.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Search.aspx.cs" Inherits="Search" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
    <script>
        function CloseModal() {
            if (window.opener) {
                window.opener.returnValue = 'your return value';
            }

            window.returnValue = 'your return value';
            self.close();
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
        </div>
    </form>
</body>
</html>

Search.aspx.cs

using System;
using System.Web;
using System.Web.UI;

public partial class Search : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {

        Page.ClientScript.RegisterStartupScript(this.GetType(), "CloseScript", "closescript()", true);

    }
}

因此,您可以将值从 Modal 传递到 Opener,而不是重定向用户。

这里是另一个例子:Modal Dialog ReturnValue

希望能帮到你。

关于c# - 模式对话框未关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15086195/

相关文章:

javascript - 什么是 Object.__proto__ 以及它与 Object.prototype 有何不同

javascript - 将 JavaScript 变量传递给辅助方法

javascript - 使用 jquery.animate 多次调用后动画延迟

c# - 如何在Asp.net中的.cs文件后面的代码中调用javascript函数

asp.net - 获取成员组 “Insufficient privileges to complete the operation”

c# - Response.Redirect 仅适用于一页,其余工作正常

c# - 将任何带有变音符号的传入 URL 重定向为等效的没有变音符号的 URL

c# - GRPC 异步响应流 C#

c# - 用作另一个 IdentityServer 的外部身份提供者的 IdentityServer 无法正确重定向

javascript - 根据单击的元素对不同数据使用相同的 jQuery 函数