Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

asp.net - Counting characters in a textbox

Posted on 2003-03-28
12
Medium Priority
?
696 Views
Last Modified: 2013-12-03
Hi,

I am trying to write a simple piece of code that updates the value of a label to be the length of the string contained within a textbox.  I am using the onKeyUp JavaScript function (that will run my own little procedure to do the counting) within the <asp:Textbox...> declaration.

However I keep getting the error '<labelname> is null or not an object'>

Can someone show me how to do this trivial task with C# as the language ?

Cheers.
0
Comment
Question by:mrwad99
  • 5
  • 4
  • 2
  • +1
12 Comments
 
LVL 6

Expert Comment

by:thomasdodds
ID: 8224636
post your code... i don't know if you are using codebehinds or not ...
0
 
LVL 19

Author Comment

by:mrwad99
ID: 8224661
Cheers.

<html>
<head>
    <script language="JavaScript">
function count()
{
    document.form1.Counter.Value = document.form1.SubComment.Value.Length;
}
</script>
</head>
<body bgcolor="#000000">
    <form name="form1" runat="server">
        <p>
            <strong><font color="white">Your name:</font></strong>&nbsp; <strong></strong>
        </p>
        <p>
            <asp:TextBox id="NameBox" runat="server" Width="223px" MaxLength="50"></asp:TextBox>
            <asp:RequiredFieldValidator id="NameValidator" runat="server" ErrorMessage="* Please give your name" Display="Dynamic" ControlToValidate="NameBox" Font-Bold="True"></asp:RequiredFieldValidator>
        </p>
        <p>
            <strong><font color="white">Your email address (optional):</font></strong>
        </p>
        <p>
            <asp:TextBox id="EmailBox" runat="server" Width="227px" MaxLength="50"></asp:TextBox>
        </p>
        <p>
            <strong><font style="BACKGROUND-COLOR: black" color="white">Your comments</font>:</strong>&nbsp;
            <asp:RequiredFieldValidator id="CommentValidator" runat="server" ErrorMessage="* Please give a comment" Display="Dynamic" ControlToValidate="CommentBox" Font-Bold="True"></asp:RequiredFieldValidator>
            <strong></strong>
        </p>
        <p>
            <asp:TextBox id="CommentBox" onkeyup="count()" runat="server" TextMode="MultiLine" Width="235px" Height="153px" MaxLength="248"></asp:TextBox>
            <asp:Label id="Counter" runat="server" Width="35px" Font-Bold="True" ForeColor="White">248</asp:Label>
        </p>
        <p>
            <asp:Button id="SubComment" runat="server" Text="Add Comment"></asp:Button>
        </p>
    </form>
</body>
</html>
0
 
LVL 2

Expert Comment

by:SWortham
ID: 8225804
Here ya go:

<%@ Page Language="C#" %>
<html>
<head>

<script language="JavaScript">
function count()
{
  Counter.innerHTML = document.form1.CommentBox.value.length;
}
</script>

</head>
<body bgcolor="#000000" onload="count()">
   <form name="form1" id="form1" runat="server">
       <p>
           <strong><font color="white">Your name:</font></strong>&nbsp; <strong></strong>
       </p>
       <p>
           <asp:TextBox id="NameBox" runat="server" Width="223px" MaxLength="50">
           </asp:TextBox>
           <asp:RequiredFieldValidator id="NameValidator" runat="server" ErrorMessage="* Please give your name" Display="Dynamic" ControlToValidate="NameBox" Font-Bold="True">
           </asp:RequiredFieldValidator>
       </p>
       <p>
           <strong><font color="white">Your email address (optional):</font></strong>
       </p>
       <p>
           <asp:TextBox id="EmailBox" runat="server" Width="227px" MaxLength="50">
           </asp:TextBox>
       </p>
       <p>
           <strong><font style="BACKGROUND-COLOR: black" color="white">Your comments</font>:</strong>&nbsp;
           <asp:RequiredFieldValidator id="CommentValidator" runat="server" ErrorMessage="* Please give a comment" Display="Dynamic" ControlToValidate="CommentBox" Font-Bold="True">
           </asp:RequiredFieldValidator>
           <strong></strong>
       </p>
       <p>
           <asp:TextBox id="CommentBox" onkeyup="count()" runat="server" TextMode="MultiLine" Width="235px" Height="153px" MaxLength="248">
           </asp:TextBox>
           <asp:Label id="Counter" runat="server" Width="35px" Font-Bold="True" ForeColor="White" />
       </p>
       <p>
           <asp:Button id="SubComment" runat="server" Text="Add Comment">
           </asp:Button>
       </p>
   </form>
</body>
</html>
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 2

Expert Comment

by:SWortham
ID: 8225847
ASP.Net was creating the form name as _ct10 because you didn't specify the ID of the form.  It's kinda weird like that.  So, I just made the Name and the ID equal to form1, and it works.  Oh, and it also creates labels as <span></span> in HTML.  So, I had to change your javascript function.  I also modified the body tag to run the counter() function when the page loads.  So... I am guessing that this is what you want.
0
 
LVL 4

Expert Comment

by:farzinm
ID: 8225904
SWortham
I was wondering why Counter is referred to as
Counter.innerHTML
while CommentBox is referred as document.form1.CommentBox...
is it becoz
"it also creates labels as <span></span> in HTML".
??
0
 
LVL 19

Author Comment

by:mrwad99
ID: 8226094
SWortham that works thanks a lot.

However for some strange reason I can enter an infinite number of characters (well, more than the maxLength value of 248) into the TextBox 'CommentBox'.

Why on earth is this ?  MaxLength works fine for the one line text boxes !

???
0
 
LVL 19

Author Comment

by:mrwad99
ID: 8226103
As for your Q farzinm,

I think that is just the syntax for handling asp labels.  Correct me if I am wrong SWortham.
0
 
LVL 2

Expert Comment

by:SWortham
ID: 8226124
Yeah.  If you look at the HTML that is generated then you'll see what I mean.  

But the label has to be controlled with Counter.innerHTML because it is inside <span id="Counter"></span>.  You can't control that with Counter.value.  Counter.value would only be used for form elements.
0
 
LVL 4

Expert Comment

by:farzinm
ID: 8226179
Thanks for the explanation. :)
0
 
LVL 2

Accepted Solution

by:
SWortham earned 120 total points
ID: 8226257
And I just read that the MaxLength property does not apply to Multiline form elements in ASP.Net.  So, it has to be done with a little custom Javascript.

Just replace your count() function with this one:

<script language="JavaScript">
function count()
{
  var iMaxLength = 248;
 
  if (document.form1.CommentBox.value.length > iMaxLength)
    document.form1.CommentBox.value = document.form1.CommentBox.value.substring(0, iMaxLength);

  Counter.innerHTML = document.form1.CommentBox.value.length;
}
</script>
0
 
LVL 19

Author Comment

by:mrwad99
ID: 8226603
Brilliant answers SWortham; I have increased the points awarded to you to 30 for the JavaScript given to manually restrict the length of the data input into the textbox.

Thanks again !
0
 
LVL 2

Expert Comment

by:SWortham
ID: 8226767
Cool, thanks.  And I'm glad I could help.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question