Abhijit_Srikumar
Abhijit_Srikumar

Reputation: 106

Restrict multiple events in ASP .net & Javascript

I have a text box with a text change event attached with it;

 <asp:TextBox ID="txtLoadPortCode" MaxLength="8" runat="server" ToolTip="Load     Port Code" CssClass="form-control form-control-Mandatory" OnTextChanged="txtLoadPortCode_TextChanged" AutoPostBack="true"></asp:TextBox>

On text change it fires a server side event to fetch the Name from Database, if Name is missing it shows a Javascript alert triggered from server side.

ScriptManager.RegisterStartupScript(this, GetType(), "OriginPortMissing", "$(function(){alert(\"This combination '" + this.txtLoadPortCode.Text + "' of Country, City & Site is not available in Master table. Please provide a valid Code.\");});", true);

I also have a submit button which has a Javascript field validation attached with it;

<asp:LinkButton ID="btnSubmitCreateRequest" runat="server" CssClass="btn btn-primary btn-custom btn-Custom-Link" OnClick="btnSubmitCreateRequest_Click" OnClientClick="return ValidateFields();">
    <i class="fa fa-paper-plane" aria-hidden="true"></i>
    <span>Submit Booking</span>
</asp:LinkButton>

On ValidateFields() function I have written field validation for multiple controls and If any validation fails it shows error message in 'alert' window.

So the scenario is, if User gives some value in text-box and directly clicks on the Submit button then both text change event and Submit button javascript validation gets fired, resulting double alert message at the same time (IE 11 browser).

Is there any way I can restrict Submit button validation event?

Upvotes: 3

Views: 177

Answers (3)

Tummala Krishna Kishore
Tummala Krishna Kishore

Reputation: 8271

Initally Don't Show the LinkButton , set it to visible="false", after the Text Change (I mean in the Textchanged event) try to set LinkButton the visible="true"

Upvotes: 3

aditya srivastava
aditya srivastava

Reputation: 723

For this just time limit or just delay the validation by using setTimeOut

setTimeOut(function(){ put your validation code },10000)

Upvotes: 1

Samir
Samir

Reputation: 1368

There is provision in Jquery. by using event.stopImmediatePropagation() . https://api.jquery.com/event.stopimmediatepropagation/][1] Please check this once. This might help you.

Upvotes: 0

Related Questions