Redirect to another page on click of OK button using javascript dynamically


In this article we are going to look at how to redirect user to another page on click of ok button using javascript dynamically. We are going to use ScriptManager.RegisterStartupScript method to show javascript alert message at run time.

In this article we are going to look at how to redirect user to another page on click of ok button using javascript dynamically.

The requirement is on click of a button, user should be shown a custom message and when the user clicks on ok. It should be redirected to another page and when the user clicks on cancel, it should not perform any action.

Assuming that there are 2 webpages in our webapplication: Default.aspx and Default2.aspx. There is a button in Default.aspx page as shown below:


<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />


When the user clicks on this button, click event of this button is fired and user is shown a javascript alert message box dynamically. When the user clicks on 'ok', user is redirected to Default2.aspx page of same web application. When the user clicks on 'cancel', nothing happens.


protected void Button1_Click(object sender, EventArgs e)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "alertmessage", "javascript:if (confirm('Details inserted successfully')) { location.href = 'http://localhost:1321/WebSite2/Default2.aspx'; }", true);

}


Article by Vaishali Jain
Miss. Jain Microsoft Certified Technology Specialist in .Net(Windows and Web Based application development)

Follow Vaishali Jain or read 127 articles authored by Vaishali Jain

Comments

Guest Author: Hari12 Aug 2013

Primary action is to save the document:

input type="button" value="Save" name="btnSave" onclick="javascript: {ddwrt:GenFireServerEvent('__commit')}" /

Now post this action, I want it to redirect the page to default.html

How do I do it?



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: