ASP.Net确认框问题

时间:2014-01-28 13:04:35

标签: c# javascript asp.net

在我的asp.net应用程序中,我使用了JavaScript确认框进行用户确认。 这是代码:

编辑:

<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" OnClientClick="return confirm('Are you sure you want to proceed with the delete?');"/>

只有在用户点击Button1_Click时才会调用OK。但是我有更复杂的逻辑,它会改变用户的重放。这是代码:

  protected void Button1_Click(object sender, EventArgs e)
    {      
        // I want to catch replay of this confirm box     
        this.Page.ClientScript.RegisterStartupScript(this.GetType(), "Termination Failed", "confirm('This Contract Have a Pre-Renewed,Please Delete that Contract First')",true);

    //If User Clicks ok
     doWork();

    //else
    doWork2();            
    }

但我无法得到用户回复。如何捕获用户响应?任何人都可以帮忙吗?

2 个答案:

答案 0 :(得分:0)

通过使用javascript确认方法,您只能决定是否要继续 - 因此只有在用户点击确定时才会调用Button1_Click方法。

你想要做的是完全不同的。我建议摆脱ASP.NET Button控件并使用&#34; common&#34; HTML链接如:

<a href="#" id="decideLink">Click me</a>

然后在javascript中

document.getElementById('decideLink').onclick = function()
{
    if(proceed) {
        // do something here
    } else {
        // do something different
    }
}

要从javascript调用代码隐藏方法,请检查此article about calling web methods from javascript

答案 1 :(得分:0)

简单的解决方案使用javascript并调用OnClientClick

<script type="text/javascript">
        function confirm() {
           if (confirm('This Contract Have a Pre-Renewed,Please Delete that Contract First')) {
                return true;
            }
            else {
                return false;
            }
        }
      </script>     
 <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" OnClientClick="return  
 confirm()"/>