微软JScript运行时错误:
问题描述:
protected void Button1_Click(object sender, EventArgs e)
{
if (bittext.Text == "")
Response.Write("<script>alert('Enter The value first or press the correct button')</script>");
else
{
b = double.Parse(bittext.Text);
Bytetext.Text = (b/8).ToString();
kbtext.Text = (b/8192).ToString();
mbtext.Text = (b/8388608).ToString();
gbtext.Text = (b/8589934592).ToString();
tbtext.Text = (b/8796093022000).ToString();
}
}
如果我没有运行在bittext.Text输入任何本网站的C#代码,这将引发一个名为微软JScript运行时错误:
微软JScript运行时错误的错误: Sys.WebForms.PageRequestManagerParserErrorException: 的从服务器收到的消息无法解析。
我该如何解决这个问题!
答
不能使用Response.Write()
对于这一点,你需要ScriptManager.RegisterStartupScript
,像这样:
ScriptManager.RegisterStartupScript(this, GetType(), "alert",
"alert('Enter The value first or press the correct button');", true);
Response.Write()
只是转储该脚本标签时,它是在响应,这使得无效/格式异常时,客户端去解析它,你想正确执行一段JavaScript ...上面是正确/提供的机制。
+1,客户对呕吐反应。此外,他可以通过在此表单上添加RequiredFieldValidator来解决此问题,并验证此垃圾客户端。 – 2010-11-18 09:57:06
同意Moo-Juice,这个验证不应该在服务器上(它应该是,但作为客户端验证的回退)。但是,这是正确的答案。 – RPM1984 2010-11-18 10:07:27
谢谢先生!有效 – 2010-11-18 10:15:31