0

我想在按钮单击时生成一个警报框。我是这样写的

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

警报框正确出现。但是我怎么能从后面的代码中获取值呢?请帮忙。

4

2 回答 2

3

当confirm返回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>
于 2013-10-23T06:02:40.627 回答
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>
于 2013-10-23T06:16:44.363 回答