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

三世轮回 提交于 2019-11-29 09:48:15
M4N

Do you have a click event handler (registered via jquery) which returns true? In that case, the return value of OnClientClick is ignored.

Have a look at my question (and answer): Why doesn't returning false from OnClientClick cancel the postback

for some reason, although I didn't have any jquery event handlers attached, it didn't work.

What actually worked was:

OnClientClick="if (validate_form() == false) return(false);"

Try changing it to

OnClientClick="ValidateMail(); return false;" 

Great answers. I do it this way. Same result- the OnClick event is not fired off if false is returned from Java function.

OnClientClick = "if (!ValidateDelete()) return false;"
OnClick="btnDeleteSupplier_Click"

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.

pao

Just add javascript:

Example:

javascript:return ValidateMail();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!