.NET how to avoid cancellation of Button Click event after a Textbox TextChanged event

830 Views Asked by At

I have a textbox using [OnTextChange] event (VB.NET), and have to use [AutoPostBack="True"] to check itself value from DB:

<asp:TextBox ID="Item1" TabIndex="7" runat="server" AutoPostBack="True"></asp:TextBox>

Scenario is:

  • Inputs anything on textbox and not yet focus out of textbox.
  • Click any button in same form of that textbox.

Unwanted Result:

  • Event [On Click] of that button has been canceled, only OnTextChange event fired.
  • Have to click on that button again.

Wanted Result:

  • Event [On Click] of button will be fire after event OnTextChanged fired

I searched around google and known that's an issue with .NET event. But still doesn't findout any helpful solution.

Please help.

2

There are 2 best solutions below

1
BGD On

If you are writing a web application, there is a property for TextBoxes CaseValidation, you can use it, or You can use text validation inside button click function than in textchanged property

0
Roger On

I just ran into the same problem. The reason the button event was not firing was that in the subroutine to process the text changed event, I had to reload the page. I combined several approaches and came up with this solution which seems to work: 1) I added a hidden field on the page which stores a state of whether or not the button has been clicked with initial value of false; 2) I added an OnClientClick to the button which calls a javascript function to set the hidden field value to true; 3) in the code behind subroutine that processes the text changed event, I check the value of the hidden field before I use a Response.Redirect to reload the page. Below are the code snippets:

<asp:HiddenField ID="addtocarthiddenfield" runat="server" Value="false" ClientIDMode="Static" />

OnClientClick="setAddCartClicked();"

<script type="text/javascript">
        function setAddCartClicked() {
            document.getElementById('addtocarthiddenfield').value = 'true';
        }
    </script>

Dim addtocart As Boolean = addtocarthiddenfield.Value


If addtocart = False Then
    Response.Redirect("MyAccount_Favorites.aspx")
End If

I hope this helps.