Abhijit_Srikumar Abhijit_Srikumar - 1 year ago 85
ASP.NET (C#) Question

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>

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?

Answer Source

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"

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download