我有一个按钮,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());
我以前使用过上面的代码并且它有效,但这次不行。 唯一的区别是我在另一个网站上使用母版页,而不是在这个网站上使用母版页,而且这个网站也有一个计时器。
<head>
中有没有和load相关的JS aspx 页面?
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);
}
}
最佳答案
尝试使用这个简单的版本
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 );
关于c# - asp.net javascript 消息未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11266382/