Solved

redirect happening before javascript function runs

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
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…

930 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

12 Experts available now in Live!

Get 1:1 Help Now