?
Solved

asp.net - Counting characters in a textbox

Posted on 2003-03-28
12
Medium Priority
?
670 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
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

WordPress Tutorial 3: Plugins, Themes, and Widgets

The three most common changes you will make to your website involve the look (themes), the functionality (plugins), and modular elements (widgets).

In this article we will briefly define each again, and give you directions on how to install them.

Question has a verified solution.

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

Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
I will show you how to create a ASP.NET Captcha control without using any HTTP HANDELRS or what so ever. you can easily plug it into your web pages. For Example a = 2 + 3 (where 2 and 3 are 2 random numbers) Session("Answer") = 5 then we…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
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

765 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