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);
}
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?