count the length of the text box on text changed event


       Me with C# How can I count the length of the text box on text changed event

Thanks in advance

Anu Palavila 


Anu Palavila
1/25/2009 12:11:27 PM 93655 articles. 5 followers. Follow

9 Replies

Similar Articles

[PageSpeed] 44

    protected void TextBox1_TextChanged(object sender, EventArgs e)
        int length = TextBox1.Text.Length;

Anas Ghanem.

Note:Please Don't hesitate to click "Report Abuse" link if you noticed something wrong on the forums (like duplicate ,Off-topic,offensive,or any post that violates the website "TERMS OF USE"). -- Thanks!

1/25/2009 12:28:32 PM

in the event add:


 this will return you the length of the changed Textbox

1/25/2009 12:31:24 PM


<input type="text" id="textbox1" onkeyup="displayNumberCount();" />

<div id="msg">



function displayNumberCount()



'msg').innerHTML = document.getElementById('textbox1').value.length





Thanks & Regards,
Izhar Ul Islam Khan
Microsoft Certified Technology Specialist
1/25/2009 1:17:44 PM

but anas I was trying to show the textbox length in label I tried like as u said

protected void TextBox1_TextChanged(object sender, EventArgs e)
int length = TextBox1.Text.Length;

 but the length is not displaying in the label on the textchage

Anu Palavila
1/28/2009 5:41:51 AM

 Hi anupalavila,

The code is simple and must work well,

Please make sure that you are changing the Label text from another code block ...

What about trying to write the lenght to the response stream ?

protected void TextBox1_TextChanged(object sender, EventArgs e)
           int length = TextBox1.Text.Length;
Note: Also , you need to make sure that the TextChanged handler is being called on the TextChanged (or after the postback )
and don't forget to assing the TextChanged handler with the TextBox 
<asp:TextBox ID="TextBox1" runat="server" ontextchanged="TextBox1_TextChanged" ....


Anas Ghanem.

Note:Please Don't hesitate to click "Report Abuse" link if you noticed something wrong on the forums (like duplicate ,Off-topic,offensive,or any post that violates the website "TERMS OF USE"). -- Thanks!

1/28/2009 9:33:34 AM


int length = TextBox1.Text.Length;


This gives the Error Cannot implicitly convert type 'int' to 'string' 

Further I think you should need to set AutoPostBack="true"  such that if focus is lose it execute your code


We All move to our original place.
Don't forget.

Imran Baloch
1/28/2009 9:57:38 AM

You're getting "Cannot implicitly connvert type int to string" because you're trying to assign an int value to the Text property of your label, which expects a string. In order to fix that, just cast the length to string before assigning it:

Label1.Text = length.ToString();

But, about setting AutoPostBack = true, I don't think it is good thing, because everytime the user types in anything he's gonna have to sit and wait all the roundtrip to the server, and this will consume server resources. As it is a web projetc (right?), you're better off handling this with javascript, which is ClientSide tecnology.

Consider the following example: (just paste it into a blank txt file and save it as AnyThing.html, and run it, then type something in the textbox)

window.onload = function()
document.getElementById('bla').onkeyup = function(e)
var e = e || window.event;
var target = || e.srcElement;

document.getElementById('counter').firstChild.nodeValue = target.value.length + ' caracteres';
<input id="bla" width="400" />
<span id="counter">.</span>
1/28/2009 11:02:56 AM

Anu It is gud that u tried this...


logically we should not do every thing in code behind..

if you will count your string in this way so on each text change page will be postbacked.. and it will make unneccary trips to the server.. which is wrong..

now you want to show these values in label.. so you can use span tag with some id.... and perform the same thing in javascript.. as somebody has mentioned in the above posts...

because when you generate your page on the browser LEBAL control gets converted in SPAN..


"Mark as answered if you feel this helps you"
"Curiosity is Bliss"

Ravi Kant Srivastava
(Sr. Software Engineer)
Connexxions Business
1/28/2009 11:57:52 AM

 try like this,

label1.Text = Length.ToString(); 


Mark as answer if you got answer from this post 

Ravi Kishore.Ch

Mark as "ANSWER" if you got the solution from this post.
1/28/2009 12:03:46 PM

Similar Artilces:

Check if the text box "text" has changed (RePost from web forms forum)
Greetings! How can i know if the "text" inside of a text box has changed? For example, I currently have this: if (txtMU.TextChanged()) { } But this does not work. The other idea I had was to assign the existing value of the text box to a string and then when the user click the save button I can create a conditional statement to compare the original string with the new string. However, I would like something more effective as I have like 12 text boxes to compare. Thanks, EduardoThanks,Please Mark this post as answered if it was helpful. Here is the easiest way that I have ...

Text box
I have a Popup calendar that opens when i click on the parent form and, and after selecting date - it fills the parent textbox and closes the popup window. Every thing is working fine, but i have one problem, i want the Textbox - textchanged event to fire when there is a change,(auto postback for textbox is set to true). This is not happening. If i manually type in the date in the textbox and go to other control then it fires, but not when i select date from PopUp. How to make it work Appreciate any response. Thanks. Parent Form JAVASCRIPT function OpenCalendar(...

how to call a text changed event in text box
hi all   I am developing a web application. In that i have used a text box and pop up calendar to generate a date time picker. Anyhow its working fine. But when i change the date in the text box, the text changed event is not calling. Also i have done autopostback = true. Can anybody help me regarding this. thanks in advance. hi Sancti, The another way is to write the ontext change event code inside the onselectedchange event of the calendar.Please do not forget to click Mark As Answer on the post that helped you. Regards,K.Sivakumar  Is u added this handler if yes and ...

Text Changed Event For Dynamic Text Box
Hi.. i'm using the Grid View in my Web Application.. I'm creating a TextBox dynamically inside the grid view using Itemplate.. now what my requirement is after entering some values in the Textbox., in the Text_changed event it will do some calculations... But now my problem is i don know how to Write the TextBox_Changed event for the Dynamically created textbox.. pls help me Hi, in ITemplate's implementation of InstantiateIn (where you also create the TB), you could write handler for it tb.TextChanged += new EventHandler(tb_TextChanged); and so on... if you need to write it...

Load Text box of one web form with selected list item of list box of another web form
one help please Problem is i have two web forms  1.  ForiegnkeyTest.aspx  2. foroginkeytable.aspx   step 1 1 ForiegnkeyTest.aspx one web form <head runat="server"><script language="javascript" type="text/javascript">   function popWin()   {         var confirmWin = null;      confirmWin ='foroginkeytable.aspx','anycontent',   'width=455,height=435,status');   }  </script> &...

Enabling button after text changed on text box from form view control
I'm using form view to display some text box that contains user's personal info. There are "save" buttons for each text box which are disable as default. I want to enable them when text is changed on the the text box. I don't know how to write the the JavaScript function to do this task and i also don't know how to call the function from the the textbox control (onchange or ontextchange?). Here is the formview code: <asp:FormView ID="FormView1" runat="server" DataSourceID ="personalSQL"> <ItemTemplate > <ta...

changing the text on text box
hello, i need to build an addon which takes (on mouse click) the text of any given input text box and change it. how do i get theobject of the text box and set my text to its value. thanks... ...

How do I block a user from entering text in a text box if they type text in another text box?
I have two text boxes, and want the user to complete one or the other.  How can I "grey out" one text box if a user starts typing in the other textbox?  Never make important decisions on a Monday! You can use the OnTextChanged event of the textboxes and write Javascript for both textboxes to disable the other textbox when the user starts to type in something in one textbox. The only problem with that is that once the user starts to type in something in one text box the other textbox gets disabled and locks up, so now the user HAS TO compulsarily type what he wants in th...

VB .net Web Form will not get contents of Text Box
I have created a web form with text boxes that are bound to a DataSet/DataView.  I have created button to navigate up and down the recordset.  That all works fine. I need to have the ability to edit/add text in any of the boxes and then when clicking "Save" button, it would build an UPDATE query to send to the database. When the Save button is clicked, I have coded to DIM a string var (strSQL) to use to build the SQL command.  For some reason, it will only get the text from a text box if the data was pulled the database.  If I add/edit data, then the SQL command will ha...

Passing Text of a text box in child form to a label in parent form
Hi Elite People !! I have a parent form with a number of ASP Labels and Buttons. Each ASP Label has a ASP Button next to it. What I want to acheive is when you click the button, it should open a new window with a ASP multiline text box and a "save" button. User can enter the text in the text box and when they click the SAve button, the Text in the text box of the Child window should be displayed in the label on the parent form.  I am successfully able to do this when I have a Text Box in the Parent form, but my requirement is to use Labels on the Parent Form. I am ...

Text style change on the text box
 Hi experts,I'm try to create an email system, i'm having problem on the compose new email which allow user to change their text style while they press the button, i had been totally out of idea to do it.So please give me any idea and suggestion how to do it... Thanks...Below is the basic of my code:<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="NewPost.aspx.cs" Inherits="_NewPost" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "

Day+Week Views: 40 mins.= minimum Default Event Length before text is legible in box
Name: Gordon Balderston Email: Product: Sunbird Summary: Day+Week Views: 40 mins.= minimum Default Event Length before text is legible in box - change to improve Comments: In Day and Week Wiews, event text completely obscured by blue line of event box when time set to less than 40 mins. (whether manually or in Tools-Options-General-Default Event Length). Suggested improvement: enable text to reveal itself fully within event box, compacting only when several events are crowded around same time. At present, 40 mins. is minimum time-setting which...

Bold Text For Selected text in Text Box
Hi EveryoneI have a small thing to do [at this time , very big for me]. I want to make some text string as bold which appears in a text box control for the web. It means some of the portion of the string will be bold and some will be in the normal font style. Can anyone suggest any idea to accomplish this task. [I know that we can set the font weight property for the whole text box and all the text appears in the selected font-weight]Any suggestions.BaberBaber SaeedSoftware EngineerPalmChip, Pakistan Toll Free: 0800-000-13 Ext:494Cell: 0300-6438634 Standard TextBox that comes with ASP.NET ...

Text Box Text changed firing twice
Has anyone come across this littile problem. I a using a text box to add quantities to a SQL table. The control is fired under texbox_textchanged. This runs a SQL stored procedure which inserts the data to the table. The problem is that if the return key is accidently pressed twice or too heavily, it would appear that the event is firing twice, thus doubling the value inserted into the table. Example of code: Private Sub txtquantity_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtquantity.TextChanged If txtfactory.Text = "" ...

Web resources about - count the length of the text box on text changed event -

The Day the Universe Changed - Wikipedia, the free encyclopedia
The Day the Universe Changed (subtitled "A Personal View by James Burke") is a British documentary television series written and presented by ...

How Has Facebook Changed News Delivery?
... cats — it altered the way stories are seen. Now everyone can make news. But for those who make a living by spreading news, Facebook has changed ...

Facebook changed my name to my unknown birth name, not my current legal name... - Facebook-Hilfeforum ...
I provided Facebook with my original birth certificate and my name change certificate - instead they changed my name to my birth name which I ...

West Wing Changed Me (@WWChangedMe) on Twitter
Sign in Sign up To bring you Twitter, we and our partners use cookies on our and other websites. Cookies help personalize Twitter content, tailor ...

Changed the AWS instance size on the Trex server from small to large, and now it runs much better. ;-) ...
A bunch of people made suggestions after reading today's Changes piece. The one that made the big difference came from my longtime programming ...

Economics : How has introductory economics changed as it's been taught in American universities over ...
Answer (1 of 5): Introductory economics has not changed enormously over the years. In part this is because much of introductory economics is ...

Knuckle Up #398: UFC 178, Cry Babies, HeadGames + Suddenly Your DNA Is Forever Changed… (MMA Nation) ...
Literary gunslinger Eugene S. Robinson of lends his one-of-a-kind perspective on UFC 178 up to and including the TEARS ...

In a terrible car crash, Matt Tudor's life changed instantly
In a split second, Matt Tudor's life changed completely.

Resources last updated: 12/28/2015 8:47:25 AM