I am currently working on asp.net using C# and I need to show a message box and confirm the input from user and redirect to another page, my code is something like this:
protected void Button1_Click(object sender, EventArgs e)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append("<script language='javascript'>");
sb.Append("if (confirm('YES OR NO?')){ /*some javascript code*/ }");
sb.Append("</script>");
Page.RegisterStartupScript("FocusScript", sb.ToString());
Response.Redirect("Default.aspx");
}
here the problem is directly getting redirected to next page without showing message box.
if i remove Response.Redirect("Default.aspx"); it shows message box successfully. I think the here may be is Response.Redirect() has higher precedence compared to javascript
I tried using
sb.Append("if (confirm('YES OR NO?')){ window.location.href = \"Default.aspx"; }\");
instead of using Response.Redirect() but page didn’t got redirected, what should I do to resolve this problem?
There is an option of using adding javascript in client click event
eg:-
and then in the code side simple redirect the page.