Milind Thakkar
Milind Thakkar

Reputation: 980

Conditional preventDefault ASP.Net button click

I am trying to block submit only in case a condition is met. If the condition matches it blocks the submit and works fine. But in case the condition not met, it still blocks the submit. Strange part is, it works fine when button is clicked again. I tried explicit postback as well, but no luck. I am sure I am missing something minor, but can't figure out.

My code looks like below.

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

 var valRetPax = $("#hdnRetPax").val();

 var valSelPax = $("#ddlPaxNum").val();

 var existed = valRetPax != '0' && valRetPax != valSelPax;

 alert(existed);
 var btnBack = $(this);

 if(existed)
    e.preventDefault();
 else
    __doPostBack($(btnBack).attr('name'), '');

     // Some other code after preventing the click....
});

and the button is defined as below.

<asp:Button Text="GO" runat="server" ID="GetAvailability" CssClass="btn btn-primary " OnClick="GetAvailability_Click" ClientIDMode="Static" />

Please help

Upvotes: 0

Views: 422

Answers (1)

VDWWD
VDWWD

Reputation: 35514

Why not simply return false in the click function. That is all you need.

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

    if (existed)
        return false;
});

Upvotes: 1

Related Questions