C# 如何从 Asp.net 2.0 中的代码背后的按钮单击弹出警报?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1920397/
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-06 21:50:48 来源:igfitidea点击:
How to popup a Alert on Button Click from code behind in Asp.net 2.0?
提问by Avi
I tried this code but is not working ..
我试过这段代码,但不工作..
protected void btnAbc_Click(object sender, EventArgs e)
{
string script = "<script language='javascript'>alert('abc');</script>";"
ClientScript.RegisterClientScriptBlock(this.GetType(), "Alert", script,true);
}
Pls advice.
请建议。
采纳答案by Guffa
You have double script tags. Add the script tags yourself:
你有双脚本标签。自己添加脚本标签:
protected void btnAbct_Click(object sender, EventArgs e) {
string script = "<script type=\"text/javascript\">alert('abc');</script>";
ClientScript.RegisterClientScriptBlock(this.GetType(), "Alert", script);
}
Or let the method add it:
或者让方法添加它:
protected void btnAbct_Click(object sender, EventArgs e) {
string script = "alert('abc');";
ClientScript.RegisterClientScriptBlock(this.GetType(), "Alert", script, true);
}
Not both.
不是都。
Also consider if the RegisterStartupScript
method is a better fit for what you want to do.
还要考虑该RegisterStartupScript
方法是否更适合您想要做的事情。
回答by ACP
ScriptManager.RegisterStartupScript(this, this.GetType(), "alerts", "javascript:alert('hai')", true);
or
或者
string script = "alert(\"Hello!\");";
ScriptManager.RegisterStartupScript(this, this.GetType(), "ServerControlScript", script, true);