In this article I will explain how to display JavaScript alert message box and the redirect to another page or website when OK Button is clicked.
 
HTML Markup
The HTML Markup consists of an ASP.Net Button which when clicked will raise a server side event (explained later).
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:Button ID="btnRedirect" runat="server" Text="Show alert and Redirect"
        onclick="btnRedirect_Click" />
    </form>
</body>
</html
 
 
Display JavaScript Alert Message Box and Redirect to another page or website
The below Click event handler has 3 variables
message – The message to be displayed in the JavaScript Alert Message Box
url – URL of the page or website when the user will be redirected once he clicks OK button
script – The JavaScript code that will display the Alert Message Box and then redirect.
C#
protected void btnRedirect_Click(object sender, EventArgs e)
{
    string message = "You will now be redirected to ASPSnippets Home Page.";
    string url = "//www.aspsnippets.com/";
    string script = "window.onload = function(){ alert('";
    script += message;
    script += "');";
    script += "window.location = '";
    script += url;
    script += "'; }";
    ClientScript.RegisterStartupScript(this.GetType(), "Redirect", script, true);
}
 
VB.Net
Protected Sub btnRedirect_Click(sender As Object, e As EventArgs)
    Dim message As String = "You will now be redirected to ASPSnippets Home Page."
    Dim url As String = "//www.aspsnippets.com/"
    Dim script As String = "window.onload = function(){ alert('"
    script += message
    script += "');"
    script += "window.location = '"
    script += url
    script += "'; }"
    ClientScript.RegisterStartupScript(Me.GetType(), "Redirect", script, True)
End Sub
 
The JavaScript function first displays the alert message box using the JavaScript window onload event, and then redirects the user to the specified page using the window location method.
 
ASP.Net: Show JavaScript Alert Message Box and Redirect to another page or website
 
 
Demo
 
Downloads