How to show message box and redirect to another page/website in visual studio using JavaScript

Introduction: In previous articles i explained how to Show jQuery notification pop up message box and hide after 5 seconds in asp.net and  Message box in asp.net website using JavaScript . and  How to call java script function from code behind file in asp.net and  how to redirect visitor from one website to another website using java script  and Validate file extension while uploading image or file through File Upload control using JavaScript in asp.net | Use of CustomValidator control to validate fileextension before uploading image or file through FileUpload control in asp.net

Now in this article i will explain How to display/show message box and redirect to another page/website in asp.net  using JavaScript.Some times it is required to show message box and then redirect to another page while creating asp.net application.Below is the simple example to do this using JavaScript.

Implementation: Lets create an example to see the concept in action:

Asp.Net C# Code to show message box and redirect to webpage/website

 protected void btnMsg_Click(object sender, EventArgs e)
    {
        Page.ClientScript.RegisterStartupScript(this.GetType(), Guid.NewGuid().ToString(), "alert('Record saved successfully.');window.location='default2.aspx';", true);
}
  • If Using Update panel then use below script:
 protected void btnMsg_Click(object sender, EventArgs e)
    {
 ScriptManager.RegisterStartupScript(this, this.GetType(), Guid.NewGuid().ToString(), "alert('Record saved successfully.');window.location='default2.aspx';", true);
}


Asp.Net VB Code to show message box and redirect to webpage/website

Protected Sub btnMsg_Click(ByVal sender As ObjectByVal e As System.EventArgsHandles btnMsg.Click
Page.ClientScript.RegisterStartupScript(Me.[GetType](), Guid.NewGuid().ToString(), "alert('Record saved successfully.');window.location='default2.aspx';", True)
 End Sub

  • If Using Update panel then use below script:
Protected Sub btnMsg_Click(ByVal sender As ObjectByVal e As System.EventArgsHandles btnMsg.Click
ScriptManager.RegisterStartupScript(MeMe.GetType(), Guid.NewGuid().ToString()"alert('Record saved successfully.');window.location='default2.aspx';"True)
 End Sub

Note: This code will redirect you to default2.aspx page after showing message box. You can also redirect visitor to another website. To do so just replace the default2.aspx with http://www.webcodeexpert.com/.

Now over to you:
If you like my work; you can appreciate by leaving your comments, hitting Facebook like button, following on Google+, Twitter, Linked in and Pinterest, stumbling my posts on stumble upon and subscribing for receiving free updates directly to your inbox . Stay tuned and stay connected for more technical updates
Previous
Next Post »

10 comments

Click here for comments
September 18, 2013 ×

Your welcome Saagar Soni..keep reading..:)

Reply
avatar
Unknown
admin
October 01, 2013 ×

Can please explain how to create post a ad then show it into posted list and hide and show as require. i will thankful you. I regularly read you blog..

Reply
avatar
Mehedi Hasan
admin
November 11, 2013 ×

Nice Blog Really Helpful ... Thank you

Reply
avatar
November 11, 2013 ×

Your welcome Mehedi Hasan..keep reading and stay tuned for more useful updates like this,,:)

Reply
avatar
Anonymous
admin
November 26, 2013 ×

Hi, I'm using VB.net & I tried putting ur statement ScriptManager.RegisterStartupScript(Me, Me.GetType(), "popup", "alert('Record saved successfully.');window.location='default2.aspx';", True)
in button_click, but no response.

Any idea y? Thanks.

Reply
avatar
Unknown
admin
December 11, 2013 ×

sir i want to use status update bar same as use in facebook,how can we build this box?

Reply
avatar
April 09, 2014 ×

Hi Lalit Sir,
I am Your New Member
Nice Code It Is......

Reply
avatar
April 09, 2014 ×

Hi,
Lalit Sir Nice Code It is........

Reply
avatar
April 10, 2014 ×

Thanks Ravi for appreciating my articles..it is always nice to hear that my article helped anyone in learning asp.net..stay connected and keep reading..:)

Reply
avatar

If you have any question about any post, Feel free to ask.You can simply drop a comment below post or contact via Contact Us form. Your feedback and suggestions will be highly appreciated. Also try to leave comments from your account not from the anonymous account so that i can respond to you easily..