javascript - 从 C# 后端发送脚本到浏览器

标签 javascript c# webforms

我有一个警报,我只想在用户第一次访问网站时向其发送警报。条件是用 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/

相关文章:

Javascript:使用不规则键循环遍历对象内的所有记录

javascript - jQuery 如何将唯一 ID 添加到动态表单输入字段

c# - 将用户的登录方法保存在cookie中?

c# - 如何修复 GattStatus : 3 - WriteNotPermitted exception for BLE Xamarin forms application?

javascript - 即使 PHP 返回正确的数据,jQuery .post 也没有响应

javascript - javascript es6 迭代器惰性求值在内存中如何工作?

c# - c#调用基类构造函数

javascript - 在 ASP.NET WebForms 服务器端帖子之间持续 knockout ViewModel ...

html - 输入字段时如何让表单值消失?

sql-server - 检测到不安全的密码格式。包含不安全密码格式的成员(member)提供程序是 : MySQLMembershipProvider