Solved

redirect happening before javascript function runs

Posted on 2011-02-16
4
461 Views
Last Modified: 2012-05-11
I have the code below.

Basically it does some stuff then notifies the user via a javascript alert that the save was successfuly.

That all works fine.

I have just added the line:
Response.Redirect("InvoiceDashBoard.aspx", False)

What I wanted is once the user clicked ok to the prompt that the next line would kick in and re-direct.

The problem is that it isn't bothering to display the javascript and is re-directing straight away.

Can I stop this?

Thanks in advance.

Oh the javascript function is:
 Public Sub JavascriptAlert(ByVal message As String)
        Try
            Dim script = "<script language='JavaScript'> "
            script += "DisplayAlert('" & message & "')"
            script += "</script>"
            Me.Page.ClientScript.RegisterClientScriptBlock(Me.GetType, "DisplayAlert", script)

        Catch ex As Exception
            Throw
        End Try
    End Sub
If invoice.Querys.FindAll(AddressOf CheckForOpenQuery).Count = 0 Then
                HelperInvoice.TransferInvoiceToAccountsPayable(invoice, hdnUserID.Value)

                JavascriptAlert("Invoice Transferred To AP")

                Clear()

                Response.Redirect("InvoiceDashBoard.aspx", False)
            Else
                JavascriptAlert("Invoice cannot be passed to AP as it has open queries.")
            End If

Open in new window

0
Comment
Question by:scm0sml
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 300 total points
ID: 34905875
You can't do it like that. The response.redirect happens before the content is flushed to the browser, so the javascript will never get executed. If you want to show a message first you will need to do a client-side redirect using javascript.
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34905901
Something like:
ClientScript.RegisterStartupScript(Page.GetType(), "redirect", "alert('Hello');location.href='WebForm1.aspx';", true);

Open in new window

0
 
LVL 82

Assisted Solution

by:leakim971
leakim971 earned 200 total points
ID: 34905908
>If you want to show a message first you will need to do a client-side redirect using javascript (line 4) :
Public Sub JavascriptAlert(ByVal message As String)
        Try
            Dim script = "<script language='JavaScript'> "
            script += "DisplayAlert('" & message & "')"
            script += "location.href = 'InvoiceDashBoard.aspx';"
            script += "</script>"
            Me.Page.ClientScript.RegisterClientScriptBlock(Me.GetType, "DisplayAlert", script)

        Catch ex As Exception
            Throw
        End Try
    End Sub

Open in new window

0
 

Author Comment

by:scm0sml
ID: 34906042
Top of the class.

I didn't want to amend my javascriptalert function as this is a generic function though.

So have gone with:
Me.Page.ClientScript.RegisterStartupScript(Page.GetType(), "redirect", "alert('Invoice Transferred To AP');location.href='WebForm1.aspx';", True)
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
asp.net uploading picture to a temporary folder 4 26
Json and ajax 1 23
modify change color of text 9 37
Keyup listener getting fired on initial page load 1 16
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question