Results 1 to 8 of 8

Thread: Count the numbers of character in the textbo

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2005
    Posts
    8

    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:
    1. <asp:label  id="lblCount" runat="server" />
    2. <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

  2. #2
    Addicted Member
    Join Date
    Nov 2004
    Posts
    149

    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.

  3. #3
    Hyperactive Member
    Join Date
    Jan 1999
    Location
    North East America
    Posts
    463

    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:
    1. <asp:TextBox ID="ShortDesc" runat="server" Width="295px" OnTextChanged="shortdesc_onkeydown" MaxLength="50"></asp:TextBox>
    2.  
    3. <asp:Label ID="lblLencount2" runat="server" Height="21px"
    4.     Text="50" Width="47px" ></asp:Label>
    TMacPherson
    MIS Systems Engineer
    [email protected]


  4. #4
    Addicted Member
    Join Date
    May 2006
    Posts
    244

    Re: Count the numbers of character in the textbo

    TextBox1.Text.Length
    This will give u the count of characters in TextBox1

  5. #5
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    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>

  6. #6
    Hyperactive Member
    Join Date
    Jan 1999
    Location
    North East America
    Posts
    463

    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.
    TMacPherson
    MIS Systems Engineer
    [email protected]


  7. #7
    Hyperactive Member
    Join Date
    Jan 1999
    Location
    North East America
    Posts
    463

    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:
    1. <asp:TextBox ID="ShortDesc" runat="server" Width="295px" OnTextChanged="shortdesc_onkeydown" MaxLength="50"></asp:TextBox>
    2.  
    3. <asp:Label ID="lblLencount2" runat="server" Height="21px"
    4.     Text="50" Width="47px" ></asp:Label>
    in the textbox there is no .length or onkeydown event.
    TMacPherson
    MIS Systems Engineer
    [email protected]


  8. #8
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Count the numbers of character in the textbo

    It can. In your codebehind,

    this.multilinetextboxname.attributes.add("onkeyup","this.value.slice(0,2000);");

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width