简体   繁体   中英

postback and validation in asp.net and javascript

I have this much

HTML

<asp:Button ID="btnSaveContest" runat="server" Text="Save &amp; Publish Contest" OnClientClick=" ValidateCreateContest();" ValidationGroup="ContestAdd" OnClick="btnSaveContest_Click" CausesValidation="false"/>

JavaScript

<script type="text/javascript">
    function ValidateCreateContest() {
        Page_ClientValidate();
        if (Page_IsValid) {
            alert('page is valid');
            $('#<%=btnSaveContest.ClientID%>').attr('disabled', 'disabled');
            __doPostBack('<%=btnSaveContest.ClientID%>', '')
            return true;
        }
        else {
            alert('not valid');      
            return false;
        }     

    }
</script>

It does post back to the page, but it does not go to the server side function btnSaveContest_Click . Why ?

Don't use ClientID , use UniqueID

    if (Page_IsValid) {
        alert('page is valid');
        $('#<%=btnSaveContest.ClientID%>').attr('disabled', 'disabled');
        __doPostBack('<%= btnSaveContest.UniqueID %>', '')
        return true;
    }
    else {
        alert('not valid');      
        return false;
    }     

This question may be useful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM