如何跟踪C#中后面代码中的警报取消事件

时间:2014-07-31 12:11:31

标签: c# javascript asp.net alert

我的要求非常简单但似乎很复杂!!

我正在使用C#代码和java脚本。

我想使用带有确认/取消按钮的警报消息,我想跟踪用户点击后面的代码,如果他已按下警告消息的确认按钮或者说他是否点击了警告消息的取消按钮。< / p>

从页面后面的c#代码说我已经调用了confirmDuplicateCustomerCreation

function confirmDuplicateCustomerCreation() {
        confirm('Customer with same details already exists. Do you want to continue?')
}


protected void Button1_Click(object sender, EventArgs e) 
{ ScriptManager.RegisterStartupScript(
    this,
    this.GetType(), 
    Guid.NewGuid().ToString(),
    @"<script type='text/javascript'>confirmDuplicateCustomerCreation();</script>", 
    false); 
    Label1.Text = Convert.ToString(CustomerDuplicateFlag.Value); 
} 

<script type="text/javascript"> 
  function confirmDuplicateCustomerCreation() { 
      if(confirm('Customer with same details already exists. Do you want to continue?'))  
          document.getElementById('<%=CustomerDuplicateFlag.ClientID %>').value = 'Yes'; 
      else 
          document.getElementById('<%=CustomerDuplicateFlag.ClientID %>').value = 'No'; } 
</script> 
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" /> 
<asp:HiddenField ID="CustomerDuplicateFlag" runat="server />

那么有没有办法跟踪用户是否点击了确认或取消.... ??

1 个答案:

答案 0 :(得分:0)

您可以将确认值保存在 HiddenField 中,并自行将页面回发到服务器。

ASPX

<asp:Button runat="server" ID="Button1"
    Text="Submit"
    OnClick="Button1_Click" />

<asp:HiddenField runat="server" ID="HiddenField1"
    OnValueChanged="HiddenField1_ValueChanged" />

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
    function confirmDuplicateCustomerCreation() {
        var value = confirm('Customer with same details already exists. Do you want to continue?');
        $('#<%= HiddenField1.ClientID %>').val(value);
        // By default, OnValueChanged of HiddenField doesn't auto post back to server, 
        // so we need to trigger the post back here by ourself.
        __doPostBack('', '');
    }
</script>

代码背后

protected void Button1_Click(object sender, EventArgs e)
{
    ScriptManager.RegisterStartupScript(
        this,
        this.GetType(),
        UniqueID,
        "confirmDuplicateCustomerCreation();",
        true);
}

protected void HiddenField1_ValueChanged(object sender, EventArgs e)
{
    if (Convert.ToBoolean(HiddenField1.Value))
    {
        // User clicked OK at Confirmation   
    }
}