Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/442.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# asp.net javascript消息未显示_C#_Javascript_Asp.net - Fatal编程技术网

C# asp.net javascript消息未显示

C# asp.net javascript消息未显示,c#,javascript,asp.net,C#,Javascript,Asp.net,我有一个按钮,onclick设置为这个方法,它应该显示一个简单的JS警报弹出窗口: string message = "File is already open. <br>Please close the file and try again."; System.Text.StringBuilder sb = new System.Text.StringBuilder(); sb.Append("<script type = 'text/javascript'&

我有一个按钮,onclick设置为这个方法,它应该显示一个简单的JS警报弹出窗口:

string message = "File is already open. <br>Please close the file and try again.";
    System.Text.StringBuilder sb = new System.Text.StringBuilder();
    sb.Append("<script type = 'text/javascript'>");
    sb.Append("window.onload=function(){");
    sb.Append("alert('");
    sb.Append(message);
    sb.Append("')};");
    sb.Append("</script>");
    ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", sb.ToString());

您没有在此处编码任何内容:

sb.Append(message);
如果消息是Hello Jean d'Arc(请注意单引号),您将以以下代码结束:

alert('Hello Jean d'Arc');
我让你想象一下这个=>的结果当然是一个javascript错误

要修复此错误,请确保已正确编码参数。一种方法是将其JSON序列化:

var serializer = new JavaScriptSerializer();
sb.AppendFormat("alert({0});", serializer.Serialize(message));
另外,由于您已经包含了
标记,请确保将false作为
RegisterClientScriptBlock
函数的最后一个参数传递,以避免两次添加它们:

ClientScript.RegisterClientScriptBlock(
    this.GetType(), 
    "alert", 
    sb.ToString(), 
    false           // <!---- HERE
);
ClientScript.RegisterClientScriptBlock(
this.GetType(),
“警报”,
某人正在做某事,

false/尝试使用此简化版本

string message = "File is already open. <br>Please close the file and try again.";
ScriptManager.RegisterClientScriptBlock(
  UpdatePanel1, // replace UpdatePanel1 by your UpdatePanel id
  UpdatePanel1.GetType(), // replace UpdatePanel1 by your UpdatePanel id
  "alert", 
  string.Format("alert('{0}');",message), 
  true );
string message=“文件已打开。
请关闭该文件,然后重试。”; ScriptManager.RegisterClientScriptBlock( UpdatePanel1,//用UpdatePanel id替换UpdatePanel1 UpdatePanel1.GetType(),//用UpdatePanel id替换UpdatePanel1 “警报”, 格式(“警报({0}”);”,消息), 正确的);
你在页面上看到任何js错误吗?计时器是如何实现的?虽然你的建议对一般场景非常有效,但本例中的消息是经过harcode编码的,没有
。我只是指出了我在代码中看到的所有错误和缺陷。消息是硬编码的这一事实绝对不会让你从中解脱出来硬编码的义务。因为下一个来这里并简单地更改此消息的开发人员将得到非常糟糕的惊喜:-)我也指出了这些缺陷,因为这是一个有很多开发人员来寻求知识的参考网站。在进行代码审查时,我看到很多人都在犯这些错误,而这些错误正是很难发现的bug的根源。我想指出的是,缺乏编码并没有导致这个问题。您的回答非常完整,非常感谢。谢谢。但它不承认
为新产品线。这个标签是什么?@user1468537:用
\n
替换

。Alert box不理解html。如果我将其单独放在button_click()上,则上面的代码可以工作,但如果我将其放在catch字段中(该字段仍在button_click()方法中),则不再工作。知道为什么吗?我可以一直追踪到catch部分,它确实进入了。尝试并捕获。我想由于回发,它从来没有机会显示它?@user1468537:请显示更多代码,不确定我是否理解。
var message = "File is already open. <br>Please close the file and try again.";
var sb = new StringBuilder();
sb.Append("<script type=\"text/javascript\">");
sb.Append("window.onload=function() {");
var serializer = new JavaScriptSerializer();
sb.AppendFormat("alert({0});", serializer.Serialize(message));
sb.Append("};");
sb.Append("</script>");
ClientScript.RegisterClientScriptBlock(
    this.GetType(), 
    "alert", 
    sb.ToString(), 
    false
);
string message = "File is already open. <br>Please close the file and try again.";
ScriptManager.RegisterClientScriptBlock(
  UpdatePanel1, // replace UpdatePanel1 by your UpdatePanel id
  UpdatePanel1.GetType(), // replace UpdatePanel1 by your UpdatePanel id
  "alert", 
  string.Format("alert('{0}');",message), 
  true );