Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

redirect happening before javascript function runs

Posted on 2011-02-16
4
Medium Priority
?
464 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 1200 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 800 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…
Suggested Courses

610 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