Calling JavaScript function from code after Response.End ()

On my page .aspx

, I have div

one that I need to hide using JavaScript after the attachment is loaded. I used the below code to do this. But since it Response

enters the scene, it HideDiv()

does not start.

I even tried to make the div the control server and set the visibility to false

. I also tried to put the part ClientScript

after Response.End();

and inside the block 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
        {

        }

      

Any idea to get JavaScript to work?

+3


source to share


3 answers


Create a .aspx

Generic Handler for file upload, then you can call it something like this

ClientScript.RegisterStartupScript(this.GetType(), "Hide", "HideDiv()", true);
Response.Redirect("MyHandler.ashx")

      

OR



Js function to hide div and redirect

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

ClientScript.RegisterStartupScript(this.GetType(), "Hide", "HideDivNRedirect();", true);

      

+3


source


You can call the function from your code like this:

Form.aspx.cs



If you want to call button click

.

protected void Button_Click(object sender, EventArgs e)
{
 Page.ClientScript.RegisterStartupScript(this.GetType(), "Hide", "HideDiv();", true);
}

      

+1


source


Calling a javascript function from the code behind won't work after Response.End();

But can be used beforeunload

, it will light up afterResponse.End();

$(window).bind('beforeunload', function() {
    // here your function
});

      

+1


source







All Articles