How do I make my JavaScript in code behind work when I redirect to another page after? I have a asp button control and when I click that button I want to alert, then navigate to another page. When I have a Response.Redirect
in my code (before or after the JS code), none of the 8 tries work. When I comment that redirect out, a few (2,7 & 8) work.
//Try one
ScriptManager.RegisterStartupScript(this, GetType(), "test", "alert('test1');", true);
//Try two
ClientScript.RegisterClientScriptBlock(typeof(Page), "test", "test2");
//Try three
Page.ClientScript.RegisterStartupScript(this.GetType(), "CallMyFunction", "alertMessage()", true);
//Try four
ClientScript.RegisterStartupScript(GetType(), "CallMyFunction", "alertMessage()", true);
//Try five
ClientScript.RegisterStartupScript(GetType(), "CallMyFunction", "javascript: alertMessage(); ", true);
//Try six
ClientScript.RegisterClientScriptBlock(GetType(), "CallMyFunction", "<script>alert('test4')</script>");
//Try seven
Response.Write("<script>alert('test5');</script>");
//Try eight
string script = "alert('test6')";
ScriptManager.RegisterStartupScript(Page, Page.GetType(), "CallMyString", script, true);
response.redirect("pageUrlHere");
//With this code above, none of the js functions (alerts) work
//response.redirect("pageUrlHere");
//With this commented out, try 2, 7 and 8 work.
JS function:
function alertMessage() {
alert('test3');
}
How do I make my JavaScript in code behind work when I redirect to another page after? I have a asp button control and when I click that button I want to alert, then navigate to another page. When I have a Response.Redirect
in my code (before or after the JS code), none of the 8 tries work. When I comment that redirect out, a few (2,7 & 8) work.
//Try one
ScriptManager.RegisterStartupScript(this, GetType(), "test", "alert('test1');", true);
//Try two
ClientScript.RegisterClientScriptBlock(typeof(Page), "test", "test2");
//Try three
Page.ClientScript.RegisterStartupScript(this.GetType(), "CallMyFunction", "alertMessage()", true);
//Try four
ClientScript.RegisterStartupScript(GetType(), "CallMyFunction", "alertMessage()", true);
//Try five
ClientScript.RegisterStartupScript(GetType(), "CallMyFunction", "javascript: alertMessage(); ", true);
//Try six
ClientScript.RegisterClientScriptBlock(GetType(), "CallMyFunction", "<script>alert('test4')</script>");
//Try seven
Response.Write("<script>alert('test5');</script>");
//Try eight
string script = "alert('test6')";
ScriptManager.RegisterStartupScript(Page, Page.GetType(), "CallMyString", script, true);
response.redirect("pageUrlHere");
//With this code above, none of the js functions (alerts) work
//response.redirect("pageUrlHere");
//With this commented out, try 2, 7 and 8 work.
JS function:
function alertMessage() {
alert('test3');
}
Share
Improve this question
edited Oct 18, 2016 at 20:11
CDspace
2,68919 gold badges32 silver badges39 bronze badges
asked Dec 13, 2013 at 9:16
Niek JonkmanNiek Jonkman
4974 gold badges11 silver badges23 bronze badges
2 Answers
Reset to default 10You can try the following:
ScriptManager.RegisterStartupScript(this,this.GetType(),"redirect",
"alert('test 9'); window.location='" +
Request.ApplicationPath + "/anotherpage.aspx';",true);
Try this it will display alert and navigate It make it in separate method just to reuse again.
public void ShowAlertAndNavigate(string msg , string destination)
{
string alert_redirect_Script = string.Format(@"<script type=""text/javascript"">
alert('{0}');
window.location.href = destination;
</script>", msg);
ClientScript.RegisterClientScriptBlock(this.GetType(), "alertredirectscript", alert_redirect_Script, false);
}