开发者

ASP.NET 3.5 onClientClick return false not working

I have the following code:

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

The OnClientClick event on my BtnImprimir Button is fired OK, if i have the cookies the confirm dialog is displayed but it doesn't matter if i respond YES or No on the confirm dialog... the onClik is always executed.


Use OnClientClick = "return imprimeTesis();". You need to return the results of function to the control.

<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"/>


You need to add a return statement:

OnClientClick = "return imprimeTesis();" 


Update: In VS2017 if UseSubmitBehavior is False C# adds its own function call to the onclick event

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

So I changed return function() to if(!function()) return;

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

this fixed my problem.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜