Search code examples
c#asp.nettextchanged

Scan a bar code inside a textbox and firing textboxChanged asp.net


I have a textbox inside a update panel:

<asp:TextBox ID="txtInsert" 
             runat="server" Font-Size="Large" MaxLength="13" 
             Width="150px" onkeyup="doPostBack(this);" AutoPostBack="True"
             OnTextChanged="txtInsert_TextChanged"></asp:TextBox>

and this textbox should get the value from scanning a barcode. All the bar codes have 13 digits. in the txtInsert_TextChanged method i check if the value scanned in inside a table and display a message and delete the textbox value.

The problem is that sometimes it only reads 1 character or 4 characters, sometimes i have 13 digits but they are made out of 2 barcodes combined.

Basically i think i have to somehow increase the time for key up since it only reads part of the bar code and then combines it with a second bar code read...

and by the way : the doPostBack(this) method is here:

<script type="text/javascript">
    function doPostBack(o) {
        __doPostBack(o.id, '');
    }
</script>

Any ideas ?

PS: the scanner doesnt trow an ENTER at the end...


Solution

  • Since your bar-code reader is on the client computer, when you call __dopostback the browser loads the same page again, and during this time, all the keyboard events simulated by the bar-code reader aren't transmitted to the textbox.

    So you need to do the postback only when you have 13 characters in your textbox.

    You can modify your javascript to do something like this: (not actual javascript)

    if (this.value.length == 13) __dopostback(this);