2

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 ?

Rauf
  • 12,326
  • 20
  • 77
  • 126
  • I have had some troubles doing similar things to this. One solution I found was to do `OnClientClick="ValidateCreateContest(); return isFormValid;"`, and then in the `ValidateCreateContest();` function do `isFormValid = true`, `isFormValid = false` as required, with `isFormValid` being a global variable. – Oliver Sep 02 '11 at 09:35
  • @Oliver :- It did postback, but it did not go to the btnSaveContest_Click function... ??? – Rauf Sep 02 '11 at 09:37
  • first check if there is any javascript error in the page using error console. if you are checking it using a breakpoint then stop the project make a clean build and then again rebuild and run the project see if it helps and if this does not work try changing the default browser. – Devjosh Sep 02 '11 at 09:38

1 Answers1

4

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.

Community
  • 1
  • 1
onof
  • 17,167
  • 7
  • 49
  • 85
  • @_onof would you please tell the difference between ClientId And UniqueId.please explore your answer a bit it will help OP.Thanks – Devjosh Sep 02 '11 at 09:44
  • @Devjosh - http://www.velocityreviews.com/forums/t78409-difference-between-id-clientid-and-uniqueid.html – Rauf Sep 02 '11 at 09:48
  • http://stackoverflow.com/questions/1612016/c-asp-net-why-is-there-a-difference-between-clientid-and-uniqueid – onof Sep 02 '11 at 09:50
  • @mraufk & onof:Thanks for your explanation and help links it will surely help me – Devjosh Sep 02 '11 at 09:51