在 Response.End() 之后从代码后面调用 JavaScript 函数

Nis*_*sha 5 asp.net httpresponse c#-4.0

在我的.aspx页面中,我需要div在下载附件后使用 JavaScript 隐藏它。我使用下面的代码来完成它。但自从Response进入现场后,他们HideDiv()就没有被解雇。

我什至尝试将 div 设置为服务器控件并将可见性设置为false. 我还尝试将ClientScript零件放在块之后Response.End();和块内finally

    try{

    ClientScript.RegisterStartupScript(this.GetType(), "Hide", "HideDiv()", true);
    Response.ContentType = "application/vnd.ms-excel";
    Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename + "");
    Response.Write(tw.ToString());
    Response.End();
}
catch (System.Threading.ThreadAbortException)
        {

        }
        catch (Exception ex)
        {
            //log error
        }
        finally
        {

        }
Run Code Online (Sandbox Code Playgroud)

有什么想法可以让 JavaScript 工作吗?

Gan*_*kar 3

创建.ashx通用处理程序来下载文件,然后您可以这样称呼它

ClientScript.RegisterStartupScript(this.GetType(), "Hide", "HideDiv()", true);
Response.Redirect("MyHandler.ashx")
Run Code Online (Sandbox Code Playgroud)

或者

js函数隐藏div和重定向

function HideDivNRedirect()
{
  //hide div
window.location.href='myhandler.ashx';
}

ClientScript.RegisterStartupScript(this.GetType(), "Hide", "HideDivNRedirect();", true);
Run Code Online (Sandbox Code Playgroud)