1

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:

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

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.

Tapas Bose
  • 28,796
  • 74
  • 215
  • 331

1 Answers1

3

Try calling the function:

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

...in the Page_Load event handler.

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:

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.

Steve Wellens
  • 20,506
  • 2
  • 28
  • 69
  • Thanks for reply. But it is not working. I mean if I add this line in Page_Load then after postback the dialog window is not become visible. – Tapas Bose Mar 25 '12 at 15:22
  • Both your functions are working, you need to get them in the correct sequence at the server. – Steve Wellens Mar 25 '12 at 15:27
  • By logging I saw that the Page_Load is executing first then EMSLogin_Authenticate when I click on the login button. Can you please tell me what will be the sequence? – Tapas Bose Mar 25 '12 at 15:34
  • I have tried to add the above line in Page_Unload which is called after EMSLogin_Authenticate, but no luck, this time the dialog is showing on error but on page refresh it is again appearing :( – Tapas Bose Mar 25 '12 at 15:37
  • I added some more information to my initial post (so I could paste formatted code). – Steve Wellens Mar 25 '12 at 16:19
  • 1
    @TapasBose Check [my answer here](http://stackoverflow.com/a/22970430/2589202) , it allows a script to startup but run only once on the event. not on reoccurring postbacks. – crthompson Apr 09 '14 at 18:33