如何从alertbox获取OK和CANCEL的值

时间:2013-10-23 05:53:09

标签: c# javascript asp.net

我想在按钮点击时生成警告框。我写得像这样

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

警报框正确显示。但是我怎么能从代码中获取值?请帮助。

2 个答案:

答案 0 :(得分:3)

当确认返回false时,由于javascript中的click事件被取消,因此没有回发。如果您想在点击取消后进行回发,则需要稍微更改一下代码:

服务器端:

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

在客户端:

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

答案 1 :(得分:0)

你也可以尝试这个

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>