0

I have this webforms application that is giving me trouble understanding what I am doing wrong. I basically want that when a user changes the value of a textbox, it triggers an OnTextChanged event whenever there is any change in text. No button is involved.

What is happening is that when I click outside of the textbox after entering text, the event gets triggered which I do not want.

How to fix this? I have checked online for solutions but can't find any. The textbox id is Barcode.

<asp:PlaceHolder runat="server" ID="ErrorMessage" Visible="false">
   <p class="text-danger">
      <asp:Literal runat="server" ID="FailureText" />
   </p>
</asp:PlaceHolder>
<div class="form-group">
   <asp:Label runat="server" AssociatedControlID="Barcode" CssClass="col-md-2 control-label">Barcode</asp:Label>
   <div class="col-md-10">
      <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Always" ChildrenAsTriggers="true">
         <ContentTemplate>
            <asp:TextBox runat="server" ID="Barcode" CssClass="form-control" AutoPostBack="true" OnTextChanged="Barcode_TextChanged" />
            <p class="text-danger">
               <%: BarCodeNotFound %>
            </p>
         </ContentTemplate>
      </asp:UpdatePanel>
   </div>
</div>
<div class="form-group">
   <asp:Label runat="server" AssociatedControlID="TotalScanCount" CssClass="col-md-2 control-label">Scan Count</asp:Label>
   <div class="col-md-10">
      <asp:TextBox runat="server" ID="TotalScanCount" CssClass="form-control" />
   </div>
</div>

On the .cs side:

protected void Barcode_TextChanged(object sender, EventArgs e)
{
  //bla bla
}
8
  • Do you want the text box to fire some event as you type keys then? OnTextChanged of course ONLY triggers after the user has entered text, and then hits tab. If you not looking to trigger an event as the user types, then how are you going to know the user done entering text into that textbox then?, Right? You can certainly have server side code to trigger/run on each keystroke. Not great having "round trips" to the server even with update panel (it's slow). Hence test this on real server, since with dev the computer + browser is same computer, and it VERY much hides the performance issues here Commented Mar 1 at 19:46
  • 1
    Note near all barcode scanners have the ability to set a "end" of scan character, and often it can even be set as enter key, so then this WOULD allow you to use on-text changed, since the scanner will (can) send a return or tab char as ending char. I would check the scanner for this setting/feature, since for scanning, you don't want to have post-backs to the server for each character scanned - even with a update panel, this will run too slow with such server page post-backs (update panels STILL do a page post back and even page load fires every time. Update panel is still a post back. Commented Mar 1 at 19:49
  • In effect, if you not going to tab out of the text box then how will you know that the user (or scanner) wants to type in more characters? So, unless you tab out, how you going to know that the user (or scanner) is done typing, and that the user (or scanner) does not want to shove more chars into that text box? Commented Mar 1 at 19:55
  • @AlbertD.Kallal A user does not type into the text box per-se. I was pasting into the textbox different values to mimic what happens in the real environment. A user will have a bar code scanner, scans the value and it pastes the value to that text box. Which it does. They keep scanning every 5 seconds with different values, I want that on every text change, the event gets triggered. Now, a user on the scanner has to touch the screen outside of the textbox for the event to trigger. One click sound on the scanner and I know the entering is done. The device looks like a smartphone. Commented Mar 1 at 19:56
  • YOU might know the scanner done, but how is the text box going to know? As I stated, it don't matter if you type, cut + paste, or have a real scanner shoving chars into that text box. What is going to be your critera to "assume" the user done typing unless they tab out? Kind of hard to cook up a solution when you not provided a means to determine when you (or cut + paste) or the scanner is done typing into that text box. All of this is the same issue (don't matter HOW chars get into the text box, it how you going to determine when your done - what's the "done" criteria you going to use here? Commented Mar 1 at 19:58

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.