javascript 删除 ClientScript.RegisterStartupScript 添加的脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9860905/
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-10-26 08:04:39  来源:igfitidea点击:

Removing scripts added by ClientScript.RegisterStartupScript

c#javascriptasp.net

提问by Tapas Bose

In my application's login control, I am showing a dialog window if the login failed. In this way:

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

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);         
    }
}

The JavaScript is:

JavaScript 是:

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

Now if the login failed the dialog is showing. But The problem is if I refresh the browser window, after one login failed, the dialog again opened, since the $(".jym").dialog("open")is written in the page. Then I have tried

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

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

But no luck.

但没有运气。

Is there any way to solve this problem?

有没有办法解决这个问题?



If I use ClientScript.RegisterClientScriptBlock()this not working, I mean the dialog is not opening on error.

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

回答by Steve Wellens

Try calling the function:

尝试调用函数:

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

...in the Page_Load event handler.

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

Page_Load occurs before the button click event handler. You can verify this by adding the following code and looking in the debug/output window:

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");      
}

So erasing the script in the Page_Load event handler should clear any previous script that was loaded.

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