简体   繁体   English

如何在重定向页面之前获取警报消息

[英]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.我正在使用 vs 2010。我需要向用户显示消息并重定向页面。

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:如果你想放入 .CS 文件,试试这个:

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.请注意,我已删除脚本标签,因为最后一个参数 true 表示您不得使用脚本标签。

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.此外,您可以使用setTimeout延迟窗口打开,这可能不是一个非常糟糕的选择。

If you are Working with UpdatePanel then You have to use this : It is work with Update Panel.如果您正在使用 UpdatePanel,那么您必须使用它:它与 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.我使用Server.Transfer()代替。

Example: (from Redirecting Another Page after Messagebox - CodeProject )示例:(来自在 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.如果可能,最好的方法是将代码放入 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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