I\'m using vs 2010. I need to display the message to user and redirect the page.
I use the below line.
ScriptManager.RegisterStartupScript(this, this
Your Asking for a redirect page, below is an example of a redirect:
string page = "Login.aspx";
string myStringVariable = "Password Update!";
ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + myStringVariable + "');Response.Redirect('"+ page +"' );", true);
If you want to put in .CS file, just try this:
var page = HttpContext.Current.CurrentHandler as Page;
ScriptManager.RegisterStartupScript(page, page.GetType(), "alert", "alert('" + msg +"');window.location ='"+ aspx +"';", true);
If you are Working with UpdatePanel then You have to use this : It is work with Update Panel.
var message = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize("Bill No. : " + BillNo + " successfully generated.");
var script = string.Format("alert({0});window.location ='ChhallanPrint.aspx';", message);
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "", script, true);
You need to write:
ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", " alert('User details saved sucessfully'); window.open('frmDisplayUsers.aspx');", true);
Please note that I have removed the script tags as the last parameter true means you must not use the script tag.
This code worked for me. If you have any problem let me know. In addition you can use setTimeout
to delay the window open that might not be a very bad choice.
The best way, if possible, is to put the code into the OnClientClick.
<asp:Button OnClientClick=" alert('blah?');" runat="server" />
The problem with putting it into a startupscript is that the startupscript is run on postback, not real time. The redirect will happen before the postback. (possibly)
The other solution of course is to put the redirect into the startupscript code.
The page will post back, the script will run and then it will redirect.
Your code is opening window but your asking for a redirect, below is an example of a redirect:
ScriptManager.RegisterStartupScript(this, this.GetType(),
"alert",
"alert('User details saved sucessfully');window.location ='frmDisplayUsers.aspx';",
true);