Solved

redirect happening before javascript function runs

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
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…

776 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