ASP.NET 3.5 onClientClick返回false不起作用

时间:2011-10-06 18:22:50

标签: c# asp.net onclick onclientclick

我有以下代码:

    <script language="javascript" type="text/javascript">
    function readCookie(name)
    {
        var nameEQ = name + "=";
        var ca = document.cookie.split(';');
        for (var i = 0; i < ca.length; i++)
        {
            var c = ca[i];
            while (c.charAt(0) == ' ') c = c.substring(1, c.length);
            if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
        }
        return null;
    }

    function imprimeTesis()
    {
        valor = readCookie("Marcadas");

        if (valor == null) return true;
        var resultado = true;
        if (valor == "1")
        {
           resultado = confirm("¿Quiere imprimir todas las tesis marcadas? Si contesta no imprimirá sólo la actual ");
        }
        return resultado;
    }
</script>
<asp:Table ID="Table1" runat="server" Height="46px" Width="696px">
    <asp:TableRow runat="server">
    <asp:TableCell ID="TableCell1" runat="server">
        <asp:ImageButton runat="server" ID="BtnGuardar" style="cursor: hand"
                     onmouseover="this.src = '../images/GUARDAR2.png';"
                     onmouseout = "this.src = '../images/GUARDAR1.png';"
                     OnClick="BtnGuardar_Click"  ImageUrl="../images/GUARDAR1.png"/>
        </asp:TableCell>
        <asp:TableCell ID="TableCell2" runat="server">
        <asp:ImageButton runat="server" ID="BtnImprimir" style="cursor: hand"
                     onmouseover="this.src = '../images/IMPRIMIR2.png';"
                     onmouseout = "this.src = '../images/IMPRIMIR1.png';"
                     OnClientClick = "imprimeTesis()"
                     OnClick="BtnImprimir_Click"  ImageUrl="../images/IMPRIMIR1.png"/>
        </asp:TableCell>

我的BtnImprimir按钮上的OnClientClick事件被触发OK,如果我有cookie,则会显示确认对话框,但如果我在确认对话框中回答是或否则无关紧要... onClik始终执行。< / p>

3 个答案:

答案 0 :(得分:4)

使用OnClientClick = "return imprimeTesis();"。您需要将函数的结果返回给控件。

<asp:ImageButton runat="server" ID="BtnImprimir" style="cursor: hand"
                 onmouseover="this.src = '../images/IMPRIMIR2.png';"
                 onmouseout = "this.src = '../images/IMPRIMIR1.png';"
                 OnClientClick = "return imprimeTesis();"
                 OnClick="BtnImprimir_Click"  ImageUrl="../images/IMPRIMIR1.png"/>

答案 1 :(得分:1)

您需要添加一个return语句:

OnClientClick = "return imprimeTesis();" 

答案 2 :(得分:0)

更新:在VS2017中 如果UseSubmitBehavior为False C#将自己的函数调用添加到onclick事件

onclick="doPostBack(&#39;ctl00$ContentPlaceHolder1$AddBtn&#39;,&#39;&#39;)" 

所以我将return function()更改为if(!function())return;

onclick="if(!ValidWorkOrder()) return;__doPostBack(&#39;ctl00$ContentPlaceHolder1$AddBtn&#39;,&#39;&#39;)"

这解决了我的问题。