ASP.NET run OnClick if I have a preventDefault

0

Good community, the following happens:

<asp:Button ID="btnGuardar" runat="server" Text="Button" OnClick="btnGuardar_Click" />

<script type="text/javascript">
     $(document).ready(function () {
        $('#<%= btnGuardar.ClientID %>').click(function (e) {
            if (document.getElementById("<%=txtPatente.ClientID%>").value == "") {
                document.getElementById("<%=alertResult.ClientID%>").className = "alert alert-danger";
                document.getElementById("<%=txtPatente.ClientID%>").focus();
                $('#<%= lblEstado.ClientID %>').html("Ingrese una patente");

            }
            e.preventDefault();
        });
    });
    </script>

The script I use for a validation so I need you not to do postback .. but I also need to run the server-side code and I would not be doing it this way, any suggestions?

    
asked by Marcelo 21.07.2016 в 00:29
source

2 answers

0

You can not mix asp.net event with client-side code and they work integrated, or use events or use javascript (or jquery) code

For the statement that you make, I would recommend you evaluate the use of the $.ajax to be able to invoke webmethod of the server side, in definite you forget the asp.net events and you work with the client side event invoking the server using ajax

Calling ASP.Net WebMethod using jQuery AJAX

The button should be an html

<input type='button' value='Guardar' id='guardar' />

then in javascript

<script type="text/javascript">
     $(function () {

        $('#guardar').click(function (e) {

            if ($("#<%=txtPatente.ClientID%>").val() == "") {
                $("#<%=alertResult.ClientID%>").addClass("alert alert-danger");
                $("#<%=txtPatente.ClientID%>").focus();
                $('#<%=lblEstado.ClientID %>').html("Ingrese una patente");
                return;
            }

             $.ajax({
                type: "POST",
                url: "page1.aspx/WebMethodName",
                data: '{}',
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function(data){
                    //codigo
                },
                failure: function(response) {
                    alert(response.d);
                }
            });


        });
    });
</script>

This way you control validations on the client side and when invoking server functionality

Actually the textbox should also be html and the label replace it with a div

    
answered by 21.07.2016 / 05:32
source
0

We go in parts: "I use the script for a validation so I need you not to do a postback" : When you want to run on the client side, I recommend using the property OnClientClick :

<asp:Button ID="btnGuardar" runat="server" Text="Button" OnClientClick="return MiFuncion();" />

Replaces:

$('#<%= btnGuardar.ClientID %>').click(function (e) {

By:

function MiFuncion() {   
//y con la siguiente linea jamás hará un postback
return false;

Now "but I also need to run the server-side code" It depends on what you need, you could use the validation control ASP.net WebForms Validation and assign the logic that requires it (Recommended).

If you make the previous option half-complicated, you could also create another button next to the above-mentioned button, that button remains hidden and shows when the validation is correct (and hidden again when it is not) , ie the btnGuardar that you have would be a btnValidar and if the validation is correct, then show btnGuardar and you link it with the code-behind , you would have the user verify and then confirm.

    
answered by 21.07.2016 в 03:23