How to get alert message before redirect a page

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.GetType(), "alert", "<script> alert('User details saved sucessfully');window.open('frmDisplayUsers.aspx');</script>", true);

But I didn’t get the alert message and the page was directly redirected.

How to get alert message?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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

Method 2

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

Method 3

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.

Method 4

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

Method 5

Redirecting to Login page after password updated alert message

Dim MyScript As String = "alert('Password Updated Successfully!. Please Login Again!'); window.location = '../Login.aspx';"

And if your intent is to framebust your page into the top level, because
your page may be inside a frame that is itself inside a frame.

Dim MyScript As String = "alert('Password Updated Successfully!. Please Login Again!');window.top.location='../Logout.aspx';"
            ScriptManager.RegisterClientScriptBlock(Page, Page.GetType(), "MyScript", MyScript, True)

Method 6

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.

Method 7

I can’t use those methods successfully.
I use Server.Transfer() instead.

Example: (from Redirecting Another Page after Messagebox – CodeProject)

Response.Write("<script>alert('User details saved successfully');</script>");
Server.Transfer("frmDisplayUser.aspx");

Method 8

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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x