Monday, August 6, 2012

Window.Confirm in JavaScript.

 

Most of the time during web application development you may get a requirement to redirect the page based on user choice.

Here I am going to show you how to redirect to an another web page or site on click of a JavaScript Ok button.

If you use Window.Confirm in JavaScript you will get OK and Cancel button. So we will redirect to another page only if user click on OK button. If user Click on Cancel button then we will stay on the same page.

So test the code first we will create a simple button

Below is the .ASPX code for creating a button.

<body>
<form id="form1" runat="server">
<div>

</div>
<asp:Button ID="btnSubmit" runat="server" onclick="btnSubmit_Click"
Text="Submit" />
</form>
</body>

Now in Code behind we will write the JavaScript code to redirect to another website.

protected void btnSubmit_Click(object sender, EventArgs e)
{

ScriptManager.RegisterStartupScript(this, this.GetType(), "alertmessage", "javascript:if (window.confirm('You will be redirected to Dotnet Galaxy. Click OK to confirm')) { location.href = 'http://asheej.blogspot.in/'; }", true);

}

Now we will see how the alert will be
Cinfrim box in JavaScript

No comments: