删除ClientScript.RegisterStartupScript添加的脚本

时间:2012-03-25 14:23:57

标签: 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()这不起作用,我的意思是对话框在出错时没有打开。

1 个答案:

答案 0 :(得分:3)

尝试调用函数:

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事件处理程序中的脚本应该清除以前加载的任何脚本。

相关问题