ASP.. NET 3.5 onClientClick返回false不工作

本文关键字:false 工作 返回 onClientClick NET ASP | 更新日期: 2023-09-27 18:10:32

我有以下代码:

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

OnClientClick事件在我的BtnImprimir按钮被触发OK,如果我有cookies的确认对话框显示,但它并不重要,如果我响应是或否在确认对话框…onclick总是被执行

ASP.. NET 3.5 onClientClick返回false不工作

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

您需要添加一个返回语句:

OnClientClick = "return imprimeTesis();" 

更新:在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;)"