我有一个警报,我只想在用户第一次访问网站时向其发送警报。条件是用 c# 编写的,这就是我使用的代码。它不发送警报。我该怎么办?
if ((string)Session["alert"] == null) {
Response.Write("<script type='text/javascript'>alert('WELCOME to the website!\nEnjoy!')</script>");
Session["alert"] = "done";
}
最佳答案
如果您使用 ASP.NET Web 表单,您可以使用 ScriptManager像这样:
ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('WELCOME to the website!\nEnjoy!')", true);
如果将最后一个参数设置为 true,内容将包含在脚本标记中。
您的代码将如下所示:
if ((string)Session["alert"] == null) {
ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('WELCOME to the website!\nEnjoy!')", true);
Session["alert"] = "done";
}
关于javascript - 从 C# 后端发送脚本到浏览器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37702413/