ASP.NET Scriptmanager在代码执行后运行

时间:2015-11-10 14:14:28

标签: javascript asp.net

我正在使用ASP.net 4.5。

我有这样的方法:

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

}

问题是  this.Page.ClientScript.RegisterStartupScript(this.GetType(),“Confirm”,“Confirm();”,true); 在所有其他代码之后执行。而不是按预期的顺序。

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

}

1 个答案:

答案 0 :(得分:0)

您也可以在客户端控制流量,而不是服务器端。所以在你的Javascript中改变像这样的代码

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

希望这也可能对您有所帮助JavaScript confirm cancel button not stopping JavaScript