Count the numbers of character in the textbo
Happy Valentine to all,
I run into the deadend and hope you can help me with. I do have a textbox. I would like to display the numbers of characters when users type into this textbox.
Here is the line of my codes:
VB Code:
<asp:label id="lblCount" runat="server" />
<asp:TextBox id="txtNarrative" runat="server" TextMode="MultiLine" Width="543px" Height="476px"></asp:TextBox></P>
I want to control that users cannot type more than 2000 characters. If it reaches 2000, this txtNarrative is disable.
Thanks for your help.
le9569
Re: Count the numbers of character in the textbo
There is a MaxLength property already on the field, all you have to do is set it to 2000 and it will not go further than that.
As for the count, you will probably have to work out some javascript that will do the count for you and display each time the user typed.
Re: Count the numbers of character in the textbo
I know this is an old post but did you ever figure this out? I am re-doing some of my old asp pages to .NET web forms and I have the same issue. I did find that textarea MaxLength does not work so I need to change a lable to show the amount of characters in the textarea and to stop at 255 charaters. Any help would be appreciated. The only thing I have found on the web is from web divets and they want $10.00 for a snippet of code.(lame) also the onTextChanged does not fire off?
VB Code:
<asp:TextBox ID="ShortDesc" runat="server" Width="295px" OnTextChanged="shortdesc_onkeydown" MaxLength="50"></asp:TextBox>
<asp:Label ID="lblLencount2" runat="server" Height="21px"
Text="50" Width="47px" ></asp:Label>
Re: Count the numbers of character in the textbo
TextBox1.Text.Length
This will give u the count of characters in TextBox1
Re: Count the numbers of character in the textbo
What sort of an idiotic site would do that? Just use some javascript!
<TEXTAREA onkeyup="this.value = this.value.slice(0, 2000)"></TEXTAREA>
Re: Count the numbers of character in the textbo
Quote:
Originally Posted by mendhak
What sort of an idiotic site would do that? Just use some javascript!
<TEXTAREA onkeyup="this.value = this.value.slice(0, 2000)"></TEXTAREA>
mendhak this wont work with webforms in the <asp:></asp> tags, at least I can't get it to work. I suppose I could just keep html but I am really trying to learn .NET and use the Webforms. I have the site working as an asp page but I am trying to convert it to .NET webform and once I changed the tags to .NET tags the count senario I was using no longer worked.
Re: Count the numbers of character in the textbo
Also I would like to know how to do it with code like below, or at least know how to call a sub from this webform code. OnTextchanged does not call the shortdec_onKeydown sub.
VB Code:
<asp:TextBox ID="ShortDesc" runat="server" Width="295px" OnTextChanged="shortdesc_onkeydown" MaxLength="50"></asp:TextBox>
<asp:Label ID="lblLencount2" runat="server" Height="21px"
Text="50" Width="47px" ></asp:Label>
in the textbox there is no .length or onkeydown event.
Re: Count the numbers of character in the textbo
It can. In your codebehind,
this.multilinetextboxname.attributes.add("onkeyup","this.value.slice(0,2000);");