[英]asp.net javascript message not showing up
I have a button and onclick is set to this method which should display a simple JS alert pop up window: 我有一个按钮,并将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());
I have used the code above before and it has worked but not this time. 我以前使用过上面的代码,但它确实有效,但是这次没有。 The only difference is that I was using master pages for the other site and not for this one, and this one also has a timer going.
唯一的区别是,我在另一站点而不是本站点使用母版页,并且该页还有一个计时器。
Is there anything JS related to load in the <head>
of the aspx page? 在aspx页面的
<head>
中是否有与加载有关的JS?
protected void btnToCSV_Click(object sender, EventArgs e)
{
try
{
StreamWriter writer = new StreamWriter(@"\\server location\test.csv");
Some writer.write stuff...
writer.Close();
}
catch (Exception ex)
{
lblMessage.Visible = true;
string message = "File is already open. Please close the file and try again.";
ClientScript.RegisterClientScriptBlock(
this.GetType(),
"alert",
string.Format("alert('{0}');", message),
true);
}
}
Try with this simplyfied version 尝试使用此简单版本
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 );
You are not encoding anything here: 您在此处未进行任何编码:
sb.Append(message);
If the message is Hello Jean d'Arc (notice the single quote) you will end up with the following code: 如果消息是Hello Jean d'Arc (请注意单引号),您将得到以下代码:
alert('Hello Jean d'Arc');
I am leaving you imagine the result of this => a javascript error of course. 我让您想象这=>当然是javascript错误的结果。
To fix this error make sure that you have properly encoded the argument. 要解决此错误,请确保已正确编码了参数。 One way to do this is to JSON serialize it:
一种方法是JSON序列化它:
var serializer = new JavaScriptSerializer();
sb.AppendFormat("alert({0});", serializer.Serialize(message));
Also since you have already includeed the <script>
tags, make sure that you pass false as last argument of the RegisterClientScriptBlock
function to avoid adding them twice: 同样,由于已经包含了
<script>
标记,因此请确保将false传递为RegisterClientScriptBlock
函数的最后一个参数,以避免将它们添加两次:
ClientScript.RegisterClientScriptBlock(
this.GetType(),
"alert",
sb.ToString(),
false // <!---- HERE
);
And here's how the full code might look like: 完整的代码如下所示:
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
);
Another remark is that if you are doing this in an AJAX call you should use the ClientScript.RegisterStartupScript
method. 另一个要点是,如果在AJAX调用中执行此操作,则应使用
ClientScript.RegisterStartupScript
方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.