Solved

redirect happening before javascript function runs

Posted on 2011-02-16
4
455 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
  • 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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…

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now