2

I'm using ASP.net 4.5.

I have a method like this:

 protected void OnConfirm(object sender, EventArgs e)
        {

// this function checks if an id exist in database
            CheckIDExist(123);        


//If Id exist I want to give the popup conform prompt to user

           this.Page.ClientScript.RegisterStartupScript(this.GetType(), "Confirm", "Confirm();", true);

and then following code:

             string[]  arrReply =Request.Form["confirm_value"].Split(',');
             string confirmValue = arrReply[arrReply.Length-1];

            if (confirmValue == "Yes")
            {
                //populate the fields
                int y=1;/

            }
            else
            {

                ClearSection1();
}

}

The problem is this.Page.ClientScript.RegisterStartupScript(this.GetType(), "Confirm", "Confirm();", true); Does executed after all other codes. and not in the order expected.

function Confirm() {
    var confirm_value = document.createElement("INPUT");
    confirm_value.type = "hidden";
    confirm_value.name = "confirm_value";
    if (confirm("There is a Survey that is open for thid CID. Do you wish to continue with existing Survey? If you press on Cancel a New Survey will be started.")) {
        confirm_value.value = "Yes";
    } else {
        confirm_value.value = "No";
    }
    document.forms[0].appendChild(confirm_value);

}
S Nash
  • 2,363
  • 3
  • 34
  • 64

1 Answers1

0

Instead of server side, you can control the flow on client side as well. So in your Javascript change the code like this

function Confirm() {
    var confirm_value = document.createElement("INPUT");
    confirm_value.type = "hidden";
    confirm_value.name = "confirm_value";
    if (confirm("There is a Survey that is open for thid CID. Do you wish to continue with existing Survey? If you press on Cancel a New Survey will be started.")) {     
        return true;

    } else {
        // Below line will works
         return false;
    }        
}

Hopefully this could be helpful for you as well JavaScript confirm cancel button not stopping JavaScript

Community
  • 1
  • 1
fais
  • 165
  • 2
  • 15