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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11808329/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 19:20:31  来源:igfitidea点击:

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

c#javascriptasp.net

提问by user1529419

I have a behindcode javascript. it is to show a javascript dialog box.

我有一个隐藏代码 javascript。它是显示一个javascript对话框。

however, it keep show this error

然而,它一直显示这个错误

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

This code was put inside masterpage. I had also use the exactly same code at other aspx file, and it work out fine apart from this..

此代码放在母版页中。我还在其他 aspx 文件中使用了完全相同的代码,除此之外它运行良好..

here is my code:

这是我的代码:

   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');";

    }   

采纳答案by Brian Mains

Try:

尝试:

Page.ClientScript

instead to see if it makes a difference.

而是看看它是否有所作为。

回答by Dennis Traub

On the master page try ScriptManager.RegisterStartupScript()instead. Watch out, the signature slightly differs from Page.ClientScript.RegisterClientScriptBlock().

在母版页上尝试ScriptManager.RegisterStartupScript()。注意,签名与Page.ClientScript.RegisterClientScriptBlock().

回答by denizemektup

For cs file the sample is;

对于 cs 文件,示例是;

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

for masterpage cs the sample is;

对于 masterpage cs,示例是;

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