mercredi 27 février 2019

ASP.NET conditional yes/no messagebox

I have an asp:Button that fires a code behind function on the OnClick event. In that OnClick event several things happen, and among those things I do a check in the database for if I need to ask the user a yes or no question. For that I need a message box. First I did it like this:

protected void MyButton_Onclick(object sender, EventArgs e)
{
    // lots of stuff happening
    bool iNeedToAskTheUser = INeedToAskTheUser(stuff);
    if (iNeedToAskTheUser) 
    {
        DialogResult result = MessageBox.Show("Do you want to fix all objects?", "Fix objects", MessageBoxButtons.YesNo);
        if (result == DialogResult.Yes) // do stuff
    }
    // some other stuff
}

This works fine locally but not when deployed, so I figure I would need to use ScriptManager.RegisterStartupScript instead. I could just add javascript on the ASPX page that fires up a dialog and saves the response in a hidden control that I can then look at, but I don't want to fire up the dialog unless I have to, which I check for before I do the DialogResult in the code above. So I can't do that immediately when the user clicks the button.

Is there any way I can use ScriptManager.RegisterStartupScript in "the middle" of my _OnClick code so that I can choose whether or not to actually show the button, and then also know if the user clicked yes or no, (preferably) without doing a postback?




Aucun commentaire:

Enregistrer un commentaire