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

20

我有一个后台的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("<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()); string script = "alert('abc');";

    }   
3个回答

52

尝试:

Page.ClientScript

尝试一下看是否有所区别。


我收到了“当前上下文中不存在'Page'名称”的错误信息,有什么想法吗? - Melissa Skywalkz

8
对于 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);

感谢Denize提供详细的解释。 - Nad

3
在主页面上,请尝试使用ScriptManager.RegisterStartupScript()。注意,与Page.ClientScript.RegisterClientScriptBlock()的签名略有不同。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接