ASP.NET 3.5 onClientClick return false not working

Carlos de Luna Saenz picture Carlos de Luna Saenz · Oct 6, 2011 · Viewed 7.5k times · Source

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.

Answer

Samich picture Samich · Oct 6, 2011

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