简体   繁体   中英

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?

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

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

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.

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

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)

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM