c# - 删除由 ClientScript.RegisterStartupScript 添加的脚本

标签 c# javascript asp.net

在我的应用程序的登录控件中,如果登录失败,我将显示一个对话框窗口。这样:

protected void EMSLogin_Authenticate(object sender, AuthenticateEventArgs e) {
    log.Info("=============INSIDE EMSLogin_Authenticate======");
    RadTextBox UserName = EMSLogin.FindControl("UserName") as RadTextBox;
    RadTextBox Password = EMSLogin.FindControl("Password") as RadTextBox;

    if (Membership.ValidateUser(UserName.Text, Password.Text)) {
        FormsAuthentication.RedirectFromLoginPage(UserName.Text, false);
    } else {
        ClientScript.RegisterStartupScript(typeof(ScriptManager), "CallShowDialog", "showDialog();", true);         
    }
}

JavaScript 是:

function showDialog() {
    $(document).ready(function () {
        $(".jym").dialog("open");
    });
}

现在,如果登录失败,将显示该对话框。但问题是,如果我刷新浏览器窗口,在一次登录失败后,对话框再次打开,因为 $(".jym").dialog("open") 写在页面中。那我试过了

protected void Page_Unload(object sender, EventArgs e) {        
    log.Info("=============INSIDE Page_Unload======");
    ClientScript.RegisterStartupScript(typeof(ScriptManager), "CallShowDialog", "", true);
}

但没有运气。

有什么办法可以解决这个问题吗?


如果我使用 ClientScript.RegisterClientScriptBlock() 这不起作用,我的意思是对话框没有打开错误。

最佳答案

尝试调用函数:

ClientScript.RegisterStartupScript(typeof(ScriptManager), "CallShowDialog", "", true);

...在 Page_Load 事件处理程序中。

Page_Load 发生在按钮单击事件处理程序之前。您可以通过添加以下代码并查看调试/输出窗口来验证这一点:

protected void Page_Load(object sender, EventArgs e)
{
    System.Diagnostics.Debug.WriteLine("Page_Load");   
}

protected void Button1_Click(object sender, EventArgs e)
{
    System.Diagnostics.Debug.WriteLine("Button1_Click");      
}

因此,在 Page_Load 事件处理程序中删除脚本应该会清除所有先前加载的脚本。

关于c# - 删除由 ClientScript.RegisterStartupScript 添加的脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9860905/

相关文章:

c# - 从 MVVM WPF 项目中的 DataGrid 选择多个项目

c# - 通过扩展方法给 IFoo.Foo() 一个实现

Javascript keyup shiftKey 不是预期的行为

javascript - 使用 Angular 函数根据输入动态添加大量图像

c# - 无法加载工具箱项目。它将从工具箱中删除

c# - 滥用最小起订量来测试 if 条件

javascript - SlickGrid 2.0 无法更改奇数索引中的行背景颜色

c# - 如何使用 C#/ASP.Net 在 responseText 中传回错误

asp.net - umbraco tiny MCE 3 Internet Explorer 插入链接错误

c# - 使用 MYSQL INBUILT 函数通过选择查询过滤数据表记录