简体   繁体   English

C#中的服务器端Javascript警报

[英]Server Side Javascript Alert in C#

I'm attempting to create an alert that works on the server side, preferably using JavaScript. 我试图创建一个在服务器端有效的警报,最好使用JavaScript。 This alert is displayed if a user's input does not match a value pulled from a SQL server. 如果用户输入的值与从SQL Server提取的值不匹配,则会显示此警报。 The client side alert I was using was: 我正在使用的客户端警报是:

MessageBox.Show("User Input was not found. Please try again.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);

I attempted to replicate this by doing: 我尝试通过执行以下操作来复制它:

   string message = "User Input was not found. Please try again.";
   System.Text.StringBuilder sb = new System.Text.StringBuilder();
   sb.Append("<script type = 'text/javascript' runat = 'server'>");
   sb.Append("window.onload=function(){");
   sb.Append("alert('");
   sb.Append(message);
   sb.Append("')};");
   sb.Append("</script>");
   Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", sb.ToString());

I also tried this: 我也试过这个:

   ClientScriptManager cs = Page.ClientScript;
   cs.RegisterClientScriptBlock(
   this.GetType(),
   " ",
   @"<script language=javascript>alert('User Input was not found. Please try again');</script>",
   true
   );

After this alert is displayed, I'd like to immediately redirect using Respone.Redirect("redirectpage.aspx") . 显示此警报后,我想立即使用Respone.Redirect("redirectpage.aspx")

Is there a more effective solution to this issue? 有没有更有效的解决方案? Am I missing something from my code? 我的代码中缺少什么吗? This is my first time using JavaScript for server side applications, so any detail would be appreciated. 这是我第一次将JavaScript用于服务器端应用程序,因此任何细节都将不胜感激。

Try using this method, it also has parameter to Redirect to Particular page 尝试使用此方法,它也具有重定向到特定页面的参数

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

Call it using this, 用这个叫它

HTMLHelper.jsAlertAndRedirect(this.Page, "Success Message !", ResolveUrl("~/default.aspx"));

Use ajax 使用ajax

$.ajax({
  url: validationUrl,
  data: {paramName: paramValue }
}).done(function(data ) {
  if (data.error)
    alert("error");
});

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

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