12 Replies - 1729 Views - Last Post: 06 May 2014 - 06:29 PM Rate Topic: -----

#1 kenbbm  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 19
  • Joined: 27-June 13

Characters Count in a Textbox

Posted 05 May 2014 - 05:06 PM

I am using C# language in an ASP.NET web page. I placed a textbox and a label server control on a page. My aim is to see the length of the character immediately I type a text in the textbox. Below is my code:

protected void txtme_TextChanged(object sender, EventArgs e)
    {
        int chars = txtme.Text.Length;
        lblmsg.Text = Convert.ToString(chars);
        
        
    }


I set the AutoPostback property of the textbox to True
I ran the program and as I was typing the length was not showing immediately on the label
The length only shows when I click outside the Textbox.

I need your help, I want the length to show immediately as I type in the textbox

This post has been edited by macosxnerd101: 06 May 2014 - 04:57 PM
Reason for edit:: Please use code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Characters Count in a Textbox

#2 depricated  Icon User is online

  • DLN-000

Reputation: 676
  • View blog
  • Posts: 2,325
  • Joined: 13-September 08

Re: Characters Count in a Textbox

Posted 05 May 2014 - 07:28 PM

first real quick - wrap your code in [code] tags

I do ASP in VB.NET instead of C# at work, so I don't have a LOT of experience with C# event handling.

My educated guess though, from the sound of the behavior, is that the TextChanged event doesn't fire until the cursor leaves the textbox. What events are available for your textbox that might detect any keypress, not just the final Change event?

This post has been edited by depricated: 05 May 2014 - 07:28 PM

Was This Post Helpful? 0
  • +
  • -

#3 Blindman67  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 125
  • View blog
  • Posts: 528
  • Joined: 15-March 14

Re: Characters Count in a Textbox

Posted 05 May 2014 - 08:54 PM

Change only fires when the user us finished with the object input item. Ie hitting return, tab, or leaving the input,

Try keypress events..

BTW wrong forum but question answered.
Was This Post Helpful? 0
  • +
  • -

#4 astonecipher  Icon User is offline

  • Major DIC Head
  • member icon

Reputation: 624
  • View blog
  • Posts: 2,814
  • Joined: 03-December 12

Re: Characters Count in a Textbox

Posted 06 May 2014 - 01:03 AM

Admittedly I don't write asp , but lostfocuse is happens just before you leave a control. Textchange , unless it is different for an asp page, happens whenever the text in the control does change. A keyup event may be more useful, but the event used would work in a desktop app.
Was This Post Helpful? 0
  • +
  • -

#5 kenbbm  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 19
  • Joined: 27-June 13

Re: Characters Count in a Textbox

Posted 06 May 2014 - 03:47 PM

I am using C# language in an ASP.NET web page. I placed a textbox and a label server control on a page. My aim is to see the length of the character immediately I type a text in the textbox. Below is my javascript code:
(please the code snippet is not working in my web browser)
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
  <script type="text/javascript" language="javascript">

      $(function () {
          $("#<%=txtme.ClientID %>").keypress(function () {
              $("#<%=lblmsg.ClientID %>").text()= $("#<%=txtme.ClientID %>").text.length.toString();
          });
      });
 
    </script>


I ran the program and as I was typing the length was not showing immediately on the label

I need your help, I want the length to show immediately as I type in the textbox

This post has been edited by macosxnerd101: 06 May 2014 - 04:57 PM
Reason for edit:: Please use code tags

Was This Post Helpful? 0
  • +
  • -

#6 astonecipher  Icon User is offline

  • Major DIC Head
  • member icon

Reputation: 624
  • View blog
  • Posts: 2,814
  • Joined: 03-December 12

Re: Characters Count in a Textbox

Posted 06 May 2014 - 04:12 PM

For starters use


$(document).ready( function() {

// code

});




Then you tie in the textbox to a listener. Now it just runs when first loaded and that is it.

This is more a jQuery question than an asp.
Was This Post Helpful? 0
  • +
  • -

#7 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3369
  • View blog
  • Posts: 11,417
  • Joined: 12-December 12

Re: Characters Count in a Textbox

Posted 06 May 2014 - 04:21 PM

@astonecipher
$(function () {

is equivalent to
$(document).ready( function() {

but just putting the script at the bottom of the page will suffice.

BTW This language="javascript" is obsolete and can be removed.

This post has been edited by andrewsw: 06 May 2014 - 04:25 PM

Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10482
  • View blog
  • Posts: 38,851
  • Joined: 27-December 08

Re: Characters Count in a Textbox

Posted 06 May 2014 - 04:59 PM

Duplicate threads merged. Please avoid duplicate posting.
Was This Post Helpful? 0
  • +
  • -

#9 kenbbm  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 19
  • Joined: 27-June 13

Re: Characters Count in a Textbox

Posted 06 May 2014 - 05:14 PM

View Postmacosxnerd101, on 06 May 2014 - 04:59 PM, said:

Duplicate threads merged. Please avoid duplicate posting.

I have modified the javascript:
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
  <script type="text/javascript" language="javascript">

      $(document).ready(function () {

          $('#txtme').keypress(function () {

              var name = $('#txtme').text.length.toString();
              $('#lblmsg').text(name);

          });
      });
 
    </script>

I ran the program and as I was typing the first length appeared (that is) 1 appeared on the label control immediately I typed the first character. As I continued typing nothing appeared again on the label control.

I want as I type the first character then label control will display 1 THEN when I type the second character it will display 2 without refreshing the browser.

This post has been edited by macosxnerd101: 06 May 2014 - 05:15 PM
Reason for edit:: Added code tags again. Please learn to use code tags.

Was This Post Helpful? 0
  • +
  • -

#10 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10482
  • View blog
  • Posts: 38,851
  • Joined: 27-December 08

Re: Characters Count in a Textbox

Posted 06 May 2014 - 05:16 PM

I've added code tags again to your post. Please use code tags as such: :code:.
Was This Post Helpful? 0
  • +
  • -

#11 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3369
  • View blog
  • Posts: 11,417
  • Joined: 12-December 12

Re: Characters Count in a Textbox

Posted 06 May 2014 - 05:20 PM

var name = $('#txtme').text.length.toString();

Assuming 'txtme' is an input element this should be:
var name = $('#txtme').val().length.toString();

or
var name = ($('#txtme').val()).length.toString();

although toString() is a little redundant in Javascript.

This post has been edited by andrewsw: 06 May 2014 - 05:22 PM

Was This Post Helpful? 0
  • +
  • -

#12 kenbbm  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 19
  • Joined: 27-June 13

Re: Characters Count in a Textbox

Posted 06 May 2014 - 05:46 PM

View Postandrewsw, on 06 May 2014 - 05:20 PM, said:

var name = $('#txtme').text.length.toString();

Assuming 'txtme' is an input element this should be:
var name = $('#txtme').val().length.toString();

or
var name = ($('#txtme').val()).length.toString();

although toString() is a little redundant in Javascript.


I am now using input elements: Below is my code:
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
  <script type="text/javascript" language="javascript">

      $(document).ready(function () {

          $('#txtname').keypress(function () {

              var name = $('#txtname').val().length.toString();

              $('#txtdetails').val(name);

          });
      });
 
    </script>

    <input type="text" id="txtname" />
    <input type="text" id="txtdetails" />



I ran the program and as I was typing the length was not showing immediately on the txtdetails.
I still need your assistance...Thanks
Was This Post Helpful? 0
  • +
  • -

#13 astonecipher  Icon User is offline

  • Major DIC Head
  • member icon

Reputation: 624
  • View blog
  • Posts: 2,814
  • Joined: 03-December 12

Re: Characters Count in a Textbox

Posted 06 May 2014 - 06:29 PM

Plug this in your ready function to test functionality.



var count = 0;
$( "#txtname" ).keypress(function( event ) {

  count++;
$('#txtdetails').val(count);
});


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1