Friday, December 12, 2008

Method in .NET that uses javascript to display an alert box before redirecting to a new page

Public Sub RedirectBox(ByVal page As System.Web.UI.Page, ByVal message As String, ByVal URL As String)
Dim Script As String = "
"

page.ClientScript.RegisterStartupScript(Me.GetType, "RedirectBox", Script)
End Sub

This method is very useful when programming in .NET and you want to display an alert message in your page before the page would redirect to another page. You could call this method for example to display the success message alert after a successful transaction before going back to the home page. Usually if we just use window.location after displaying alert, the tendency is that the alert box is not displayed and the page will immediately be redirected to the new URL. Using ClientScript.RegisterStartupScript, the alert will first display and the page will only redirect after clicking the OK button of the alert box.

No comments: