【问题标题】:ASP.NET: How to pass window.ReturnValue to C# code?ASP.NET:如何将 window.ReturnValue 传递给 C# 代码?
【发布时间】:2013-02-15 06:48:12
【问题描述】:

我使用 showModalDialog 函数打开弹出窗口,即 ASP.NET 页面。在下拉列表中选择一些选项后,我填充 window.ReturnValue 并按 OK 按钮。模态弹出窗口关闭,但我不知道如何将返回值传递给后面的 C# 代码以继续进行。

代码如下:

打开弹窗:

function ClientItemClicked(sender, eventArgs)
{
    if (eventArgs.get_item().get_value() == "excel")
    {
        var retVal = window.showModalDialog("ExportToExcelChoice.aspx", null, "dialogWidth: 400; dialogHeight: 200; center: yes; resizable: no;");
    }
}

关闭弹出窗口:

function ReturnValue() {
    var choice = document.getElementById("DropDownList1").value;
    if ((window.opener != null) && (!window.opener.closed)) {
        window.ReturnValue = choice;
        var result = window.ReturnValue;
    }
    window.close();
}

我使用火狐。

【问题讨论】:

  • 也许把它写在一个隐藏的字段中,然后从那里读取?
  • 我试过了,但我不知道如何访问隐藏字段,因为隐藏字段放在用户控件内,用户控件在占位符内,占位符在内容页面内,内容页面在母版页内。我从用户控件内部打开模式窗口。你知道怎么做吗?

标签: javascript return-value


【解决方案1】:

创建服务器端隐藏输入并为其分配返回值。

使用 jQuery:

$("#<%=serverhidden.ClientID%>").val(retVal)

或javascript:

document.getElementById("<%=serverhidden.ClientID%>").value = retVal

现在在回发时,您可以从服务器端的隐藏输入中访问值。

【讨论】:

  • 是的。我已经尝试过了,但它不起作用。请用我使用的代码查看下面的答案。
  • 这是我第四天来这里寻找答案,但到目前为止没有人能提供帮助。
  • 隐藏字段如下所示:
  • 我可以使用它来使其工作:window.opener.document.getElementById("ctl00_ContentPlaceHolder1_ctl00_HiddenField2").value = choice;但是,这不是正确的方式。
猜你喜欢
  • 2012-08-21
  • 1970-01-01
  • 2021-02-11
  • 1970-01-01
  • 2021-07-18
  • 2012-10-15
  • 2018-01-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多