javascript 在 ASP.NET c# 中重定向到另一个页面之前显示 ClientScript 警报?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11993048/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 14:59:51  来源:igfitidea点击:

showing ClientScript Alerts before Redirecting to another page in ASP.NET c#?

javascriptasp.netalertresponse.redirectclientscript

提问by Victor

I gotta address a problem here...

我必须在这里解决一个问题...

I'm developing a system where I am constantly showing Messages to the user, For instance... I'm adding a new client, I input all the data and press Save, ON C# what I do is do all the process of saving, then I use...

我正在开发一个系统,在该系统中我不断向用户显示消息,例如......我正在添加一个新客户端,我输入所有数据并按保存,在 C# 上我所做的就是完成所有保存过程,然后我用...

ClientScript.RegisterStartupScript(this.GetType(), "Mensaje", "alert('Client Saved Successfully');", true);

And that's it... Eventually the message is displayed since I'm staying in the same site (because if I want to add another client, I must stay in the sme site, not send me back to my MAIN SCREEN) or at least I'm not telling the system to do a redirection...

就是这样......最终消息显示,因为我留在同一个站点(因为如果我想添加另一个客户端,我必须留在 sme 站点,而不是将我送回我的主屏幕)或至少我不是告诉系统进行重定向...

However here's another example...

然而,这是另一个例子......

If I'm going to edit a client, I'm redirected to the edit screen, I input all my changes and click SAVe, on C# I do all the process to save changes, then I use a clientscript as the one above, and then ...

如果我要编辑客户端,我将被重定向到编辑屏幕,我输入所有更改并单击“保存”,在 C# 上我执行所有过程以保存更改,然后我使用上面的客户端脚本,并且然后 ...

this.Response.Redirect("~/Main.aspx");

However, I noticed that if I use a Response.Redirect I will not be in the same page anymore, the alert won't be displayed to the user....

但是,我注意到如果我使用 Response.Redirect 我将不再位于同一页面中,警报将不会显示给用户....

This is a constant issue that I'm running into, and I would like to know what could be the solution to that...

这是我一直遇到的一个问题,我想知道解决这个问题的方法是什么......

Thank you and I hope you can help me

谢谢你,我希望你能帮助我

回答by Hanlet Esca?o

This is what I have for my projects:

这是我的项目:

public static void jsAlertAndRedirect(System.Web.UI.Page instance, string Message, string url)
{
    instance.Response.Write(@"<script language='javascript'>alert('" + Message + "');document.location.href='" + url + "'; </script>");
}

I use it like this from a page:

我从一个页面这样使用它:

HTMLHelper.jsAlertAndRedirect(this, "This is an alert.", ResolveUrl("~/Default.aspx"));

Good luck.

祝你好运。

回答by Subrat Kumar Hota

 string strScript = "&lt;script &gt;alert('The User has been added successfully.');                 window.location='AdminPanel.aspx';&lt; /script &gt;";
 Page.ClientScript.RegisterStartupScript(this.GetType(), "Startup",strScript);

回答by Subrat Kumar Hota

  string  strScript = "<script>"+"alert('The User has been added successfully.');";
                strScript += "window.location='AdminPanel.aspx';";
                strScript += "</script>";
        Page.ClientScript.RegisterStartupScript(this.GetType(), "Startup", strScript);