即使在从OnClientClick返回false之后,回发也会触发

时间:2014-05-01 06:03:19

标签: c# javascript jquery asp.net

我需要在Jquery函数返回false后停止Button1_Click fire。这是我的代码......

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <script type="text/javascript" src="http://code.jquery.com/Javascript/jquery-1.4.2.min.js"></script>
    <script src="Scripts/a.js" type="text/javascript"></script>
    <script runat="server">

        protected void Button1_Click(object sender, EventArgs e)
        {

        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="return x();"  OnClick="Button1_Click" />
            OnClick="Button1_Click" />
    </div>
    </form>
</body>
</html>

js文件

function x() {

    alert('');

    return false;
}

这是我生成的HTML文件

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1">
    <script type="text/javascript" src="http://code.jquery.com/Javascript/jquery-1.4.2.min.js"></script>
    <script src="Scripts/a.js" type="text/javascript"></script>
    <title>

</title></head>
<body>
    <form method="post" action="Default.aspx" id="form1">
<div class="aspNetHidden">
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwULLTExNTM0OTE4ODNkZI8CSkltDp/afauc6DGl2D8JvNnVI1ffgzykF0L2alVM" />
</div>

<div class="aspNetHidden">

    <input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEdAAOSROoMi8eHXeBzZ77oKwMrESCFkFW/RuhzY1oLb/NUVM34O/GfAV4V4n0wgFZHr3eJjGQabRwzK4TIMzE2tX2gN9468Yg/u6i/oj/9EvNo1w==" />
</div>
    <div>
        <input name="TextBox1" type="text" id="TextBox1" />
        <input type="submit" name="Button1" value="Button" onclick="return x();" id="Button1" />
    </div>
    </form>
</body>
</html>

我也尝试过类似的链接:

Why doesn't returning false from OnClientClick cancel the postback

在这种情况下,通过jquery注册了一个单击事件的处理程序..我认为它不在我的情况下

ASP.NET OnClientClick="return false;" doesn't work

不应该有一个单击事件处理程序(通过jquery注册)返回true .whats意味着什么?这是否意味着我需要删除onClick函数...我不想这样做

我还尝试了其他建议的代码:

<asp:Button ID="Button1" runat="server" Text="Button"  OnClientClick="if (!x()) return     
false;"  OnClick="Button1_Click" /> 

<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="javascript:return        
x();"OnClick="Button1_Click" />  

他们也没有工作

请建议

2 个答案:

答案 0 :(得分:3)

这对我有用......
.aspx page

<asp:Button id="btn" runat="server" Text="Button" onclientclick="if (!x()) { return false; }" onclick="btn_Click" />
<script type="text/javascript">
   function x() {
      return false;
   }
</script> 

Reference Help


来自外部javascript文件工作代码的

更新 external.js

function x() {
    var result = confirm("Sure");
    if (result)
        return true;
    else
        return false;
}

.aspx page

<form id="form1" runat="server">
<asp:Button id="btn" runat="server" Text="Button" onclientclick="if (!x()) { return false; }" onclick="btn_Click" />
</form>
<script src="external.js" type="text/javascript"></script>

希望它有所帮助.. !!

答案 1 :(得分:1)

您还可以将所有代码放在单个文件中。直接从jQuery附加处理程序

        jQuery(document).ready(function () {
            AttachDeleteButtonHandler();
        });

        function AttachDeleteButtonHandler() {
            var btnDelete = $("div[class='page']").find("table[id$='tblButtons']").find("input[id$='btnDelete']");
            btnDelete.click(
                function () {
                    return ConfirmDelete();
                }
            );
        }

        function ConfirmDelete() {
            var msg = $('div[id="hdnElements"]').find('span[onclick*="translatedMsgText"]').text();
            if (confirm(msg)) {
                return true;
            }
            else {
                return false;
            }
        }