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

后端 未结 7 1144
鱼传尺愫
鱼传尺愫 2020-12-18 23:55

I just want to add some client side (JQuery Javascript) validation in a web user control. I put an OnClientClick handler and the function gets called. BUT, even if I return

相关标签:
7条回答
  • 2020-12-19 00:26

    I'm writing this answer only because I'm using html buttons with ASP.NET WebForms and I couldn't find solution why should I replace my piece of code with working examples. Here is solution why it is not working. Hope it will help you to understand the issue like checking it helped me. This is my first post, so sorry for style.

    <button type="button" id="buttonAddOrEdit" class="btn btn-success"  runat="server" onclick="return myValidate()"    onserverclick="buttonAddOrEdit_ServerClick">Zapisz</button>
    

    And javascript function:

    function myValidation() {
            if (validator.form()) {
               //Project logic
                return true;
            }
            else return false;
        };
    

    Using client side click after correct validation wasn't triggering event on server side that was binded to button. Solution mentioned to change piece of code to:

    onclick="if(!myValidation()) return;"

    Works because of way that html rendered on page with onserverclick is created. Onserverclick on html button is being replaced by __doPostBack method from javascript. Fullcode of html button rendered on client side looks this way:

    <button onclick="return myValidation(); __doPostBack('ctl00$PortalContent$buttonAddOrEdit','')" id="ctl00_PortalContent_buttonAddOrEdit" type="button" class="btn btn-success">Zapisz</button>  Błąd składni
    

    And after replacing it with if statment.

    <button onclick="if(!myValidation()) return; __doPostBack('ctl00$PortalContent$buttonAddOrEdit','')" id="ctl00_PortalContent_buttonAddOrEdit" type="button" class="btn btn-success">Zapisz</button>
    

    Working with return myValidate(); won't tirgger event because it returns before __doPostBack.

    Following code will allow you to add some another code to button if neccessary and won't cause any issues.

    0 讨论(0)
提交回复
热议问题