简体   繁体   中英

How to get the value of OK and CANCEL from alertbox

I want to generate an alert box on button click. I have written like this

protected void btn_submit_click(object sender, ImageClickEventArgs e)
{
  btn_submit.OnClientClick = @"return confirm('Student has not completed all the steps? Are you sure you want to submit the details?');";
                    bool type = false;   
   if(type==true)
 {
    //If clicks OK button
  }
 else
{//If clicks CANCEL button
}
} 

Alert box comes correctly. But how could I get the values from code behind?please help.

When confirm returns false, then there is no postback since the click event in the javascript is cancelled. If you want to have the postback after clicking cancel you need to change your code a little:

serverside:

protected void Page_Load(object sender, System.EventArgs e)
{
     btn_submit.Click += btn_submit_click;
     btn_submit.OnClientClick = @"return getConfirmationValue();";
}

protected void btn_submit_click(object sender, ImageClickEventArgs e)
{

                    bool type = false;   
   if(hfWasConfirmed.Value == "true")
 {
    //If clicks OK button
  }
 else
{//If clicks CANCEL button
}
} 

on the client:

<asp:HiddenField runat="server" id="hfWasConfirmed" />
<asp:Panel runat="server">
<script>
function getConfirmationValue(){
   if( confirm('Student has not completed all the steps? Are you sure you want to submit the details?')){
       $('#<%=hfWasConfirmed.ClientID%>').val('true')
   }
   else{
      $('#<%=hfWasConfirmed.ClientID%>').val('false')
   }
   return true;
}
</script>
</asp:Panel>

u can try this also

protected void BtnSubmit_Click(object sender, EventArgs e)
{
    string confirmValue = Request.Form["confirm_value"];
    if (grdBudgetMgr.Rows.Count > 0)
    {

        if (confirmValue == "Yes")
        {


        }
    }
}    



<script type="text/javascript">
                    function Confirm() {
                        var confirm_value = document.createElement("INPUT");
                        confirm_value.type = "hidden";
                        confirm_value.name = "confirm_value";
                        if (confirm("Are you sure Want to submit all Budgeted Requirement ?")) {
                            confirm_value.value = "Yes";
                        } else {
                            confirm_value.value = "No";
                        }
                        document.forms[0].appendChild(confirm_value);
                    }
                </script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM