OnClick does not run after OnClientClick

1

I have a form with a button, when I put the button (this button actually use it for several things changing the name and using flags in the method that defines the click) this executes the JavaScript in the client but never reaches the code -behind neither in the Page_Load nor in the Click event, however the JavaScript does run.

My button:

<asp:Button runat="server" ID="btnCierre" Text="Iniciar Cierre" Style="width: 150px" Enabled="false" OnClick="btnCierre_Click" OnClientClick="return StartCierre();" UseSubmitBehavior="true" />

My JS:

    <script type="text/javascript">
    function StartCierre() {
        if (document.getElementById("<%= btnCierre.ClientID %>").value == "Aceptar") {
            document.getElementById("<%= AnimacionDeCarga.ClientID %>").style.visibility = 'visible';
            document.getElementById("<%= btnDownSistema.ClientID %>").disabled = true;
            document.getElementById("<%= btnCierre.ClientID %>").disabled = true;
            document.getElementById("<%= btnCancel.ClientID %>").disabled = true;
        }
        return true;
    }
</script>

All the JS is executed even the return of the true but in the code-behind I can put breakpoints in all the code and it never reaches any.

When I use the button for other things if it works.

    
asked by Jesus Hergueta 30.08.2017 в 22:16
source

1 answer

0

When you execute the javascript function, you deactivate the buton which prevents the server event from running:

function StartCierre() {
        if (document.getElementById("<%= btnCierre.ClientID %>").value == "Aceptar") {
            //...
            document.getElementById("<%= btnCierre.ClientID %>").disabled = true;
          //...
        }
        return true;
    }

You have to leave the active buton so that the event can be executed:

    <script type="text/javascript">
    function StartCierre() {
        if (document.getElementById("<%= btnCierre.ClientID %>").value == "Aceptar") {
            document.getElementById("<%= AnimacionDeCarga.ClientID %>").style.visibility = 'visible';
            document.getElementById("<%= btnDownSistema.ClientID %>").disabled = true;
            document.getElementById("<%= btnCancel.ClientID %>").disabled = true;
        }
        return true;
    }
</script>

But if you need to disable it, when the server event is executed, you deactivate the button:

private void btnCierre_Click(object sender, EventArgs e)
{
   //...
   //...
  btnCierre.Enabled = false;
}
    
answered by 30.08.2017 / 22:42
source