mjyazdani
mjyazdani

Reputation: 2035

run a javascript code before response direct in asp.net code behind

I have this code in .aspx page: (it prevents page from responsing without confrim)

    <script type="text/javascript">
         window.onbeforeunload = confirmExit;
         function confirmExit() {
             return 'آیا مایل به خروج از صفحه هستید؟';
         }

    </script>

I want to download a file with below code and I need to run a javascript code to disable upper code but my javascript code don't work.

ScriptManager.RegisterStartupScript(this, typeof(Page), "DisableOnbeforeunload", " window.onbeforeunload = '';", true);
Response.Redirect("doc1.docx", false);

Upvotes: 0

Views: 557

Answers (1)

jor
jor

Reputation: 2157

You need to do the redirect on client-side using JavaScript:

ScriptManager.RegisterStartupScript(this, typeof(Page), "DisableOnbeforeunload",
   "window.onbeforeunload='';window.location.href='doc1.docx';", true);

Explanation: at the point, where you do the Response.Redirect no more content (including startup scripts) are sent to the client but only the redirection header.

Upvotes: 1

Related Questions