当前上下文中不存在名称“ClientScript”

我有一个后置码javascript。 它是显示一个javascript对话框。

但是,它会继续显示此错误

The name 'ClientScript' does not exist in the current context 

此代码放在母版页中。 我也在其他aspx文件中使用了完全相同的代码,除此之外它还可以正常工作。

这是我的代码:

  protected void Button2_Click(object sender, EventArgs e) { string message = "Order Placed Successfully."; System.Text.StringBuilder sb = new System.Text.StringBuilder(); sb.Append(""); sb.Append("window.onload=function(){"); sb.Append("alert('"); sb.Append(message); sb.Append("')};"); sb.Append(""); ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", sb.ToString()); string script = "alert('abc');"; } 

尝试:

 Page.ClientScript 

相反,看它是否有所作为。

对于cs文件,样本是;

 ClientScript.RegisterClientScriptBlock(this.GetType(), "{some text for type}", "alert('{Text come to here}'); ", true); 

对于主页cs,样本是;

 Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "{some text for type}", "alert('{Text come to here}'); ", true); 

在母版页上尝试使用ScriptManager.RegisterStartupScript() 。 注意,签名与Page.ClientScript.RegisterClientScriptBlock()略有不同。

  • ScriptManager类的文档