473,500 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to show quick text change

98 New Member
Hi friends please guide me how to show the quick changing text as like shown in text editor of mobile phone(While message writing it shows no of characters left)
Sep 22 '10 #1
5 2718
Frinavale
9,735 Recognized Expert Moderator Expert
To implement this solution you will have to use JavaScript.

You will write a method that checks the length of a String in a TextBox and subtracts that value from the MaxLength of the TextBox...then displays the result in a Label.


For example, the following JavaScript will retrieve the text in the TextBox and subtract it's length from the TextBoxes MaxLength...and then display the result in an ASP.NET Label:
Expand|Select|Wrap|Line Numbers
  1.  
  2. <!-- This is all in your ASPX page -->
  3. <asp:TextBox ID="myTextBox" runat="server" MaxLength="12"></asp:TextBox>
  4. <asp:Label ID="numCharsLeftLabel" runat="server"></asp:Label>
  5.  
  6. <script type="text/javascript">
  7. function DisplayNumberOfCharactersLeft(textBox)
  8. {
  9.   if(textBox)
  10.   {
  11.     var textLength = Number(textBox.value.length);
  12.     var maxLength = textBox.maxLength;
  13.     var numCharsLeft = maxLength - textLength;
  14.     var label = document.getElementById("<%=numCharsLeftLabel.ClientID%>");
  15.     if(label)
  16.     {
  17.       label.innerHTML = numCharsLeft;
  18.     }
  19.   }
  20. }
  21. </script>
In your sever side code you have to specify that the TextBox should call the DisplayNumberOfCharactersLeft JavaScript method (passing it a reference to itself) during the JavaScript onkeyup event...like this:
[vb.net]
Expand|Select|Wrap|Line Numbers
  1. myTextBox.Attributes.Add("onkeyup","DisplayNumberOfCharactersLeft(this);")
[c#]
Expand|Select|Wrap|Line Numbers
  1. myTextBox.Attributes.Add("onkeyup","DisplayNumberOfCharactersLeft(this);");
Put this code in the Page Load event or in the Page PreRender event. You only have to apply it once unless you're placing it in an UpdatePanel.

-Frinny
Sep 22 '10 #2
jagdeep gupta
98 New Member
Thanks a lot frinny.
But i cant understand if how to use inner html and onkeyup property for textbox
Sep 27 '10 #3
NeoPa
32,557 Recognized Expert Moderator MVP
Jagdeep, have you tried following the simple instructions given?

I don't develop for .NET, but I think I could follow those instructions easily enough. It's hard to know what more you could need.
Oct 1 '10 #4
Frinavale
9,735 Recognized Expert Moderator Expert
Jagdeep,

JavaScript works with HTML elements. So, you have to know what HTML elements are used to render the ASP.NET controls in order to work with them in JavaScript.


An ASP.NET Label control is rendered as an HTML <span> element. The String that you set for the Label.Text property is placed in between the opening and closing span tags.

For example, if you have declared the following Label like so:
Expand|Select|Wrap|Line Numbers
  1. <asp:Label ID="myLabel" Text="This is the text for myLabel" runat="server" />
It is rendered like this (if you right click on the browser and click "view code" you will see the HTML for the page...)
Expand|Select|Wrap|Line Numbers
  1. <span id="myLabel" name="myLabel">
  2.   This is the text for myLabel
  3. </span>
A reference to an HTML-span in JavaScript does not have Text or Value properties that you can set; but, you can access what is between the opening and closing span tags by accessing the span's innerHTML property.

So, in this code, I retrieve a reference to the ASP.NET-Label/HTML-span-element using the JavaScript document.getElementByID method and I am setting the text that is displayed by element by setting the element's innerHTML property:
Expand|Select|Wrap|Line Numbers
  1.  var label = document.getElementById("<%=numCharsLeftLabel.ClientID%>");
  2.     if(label)
  3.     {
  4.       label.innerHTML = numCharsLeft;
  5.     }
Now, you my be confused by the <%=numCharsLeftLabel.ClientID%> part of this code. Anything between <% and %> is executed on the server... And <%= is sort hand for <%Response.Write()%>...which will print a String into the place where the code exists.

So this:
Expand|Select|Wrap|Line Numbers
  1. <%=numCharsLeftLabel.ClientID%>
Prints the ClientID string property of the numCharactersLeftLabel ASP.NET label into the JavaScript code.

The ClientID property of a Label (or any other web control) gives you access to the ID that is assigned to the HTML element that represents the ASP.NET control in the browser.

Sometimes the ID of the ASP.NET controls do not match the ID of the HTML elements that represent them in the browser.

For example, this happens if you have a Label or TextBox or something in a TemplateField within a GridView control.

Since the GridView has many rows there would be multiple HTML elements rendered using with the same ID... but ASP.NET gives the HTML elements a unique ID based on the one assigned to the ASP.NET control.

(HTML elements must have unique IDs or else the page is invalid).

This also happens if you have controls within a User Control...or if you're using Master Pages.

In order to retrieve a reference to the HTML element that you want to work with using JavaScript you need to know the unique-id given to the HTML element that represents the ASP.NET control when it is rendered. You can do this using the ClientID property of controls.

So, referring to this:
Expand|Select|Wrap|Line Numbers
  1.  var label = document.getElementById("<%=numCharsLeftLabel.ClientID%>");
The <%=numCharsLeftLabel.ClientID%> is executed on the server and prints the unique id of the HTML-span element that represents the numCharsLeftLabel ASP.NET Label into the JavaScript code.

This means that when this JavaScript is viewed in the browser it will look something like this:
Expand|Select|Wrap|Line Numbers
  1. var label = document.getElementById("numCharsLeftLabel");
The JavaScript document.getElementById method returns a reference to the span element (DOM) that is the Label control. And, as I mentioned before, you can use that reference to set the text by setting the innerHTML property for the span-element...

As for the onkeyup event.
I'm not sure what to say......
It is fired when the key pressed on the keyboard comes UP from being pressed. In other words, it is fired when someone types anything while focused on the element.

Um... yeah.
I think that's about it.

-Frinny
Oct 1 '10 #5
jagdeep gupta
98 New Member
Thanks A Lot Frinny
Oct 2 '10 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

15
13333
by: Henning Vestergaard Poulsen | last post by:
Hi, I have a problem that I hope someone can help me with. I'm building a web page with pictures I've taken with my digital camera. I have succeded making a javacript that, when clicking on a...
1
1384
by: Filip De Backer | last post by:
Hi everyone, I have some fields from a database which contain html text. How can I show this text in an asp page (<b> must be bold, red must be red text, ...)? thanks for the answers, Filip
4
1926
by: Mariame | last post by:
Hi everyone i have a textbox that is added dynamically in form load i want an event to happen when the text change how can i do it? Thx in Adv
8
1985
by: addoty | last post by:
We have a web application that has a lot of large textareas for data collection. We need to track who makes changes, when and what changes were made. Right now I'm storing the entire text in a...
2
2762
by: JLC | last post by:
Hi, I am creating a page in asp.net that has a checkbox and a textbox. When the checkbox is checked I want the textbox to become active and show text. If the checkbox is unchecked, I would like...
1
4530
by: TechnoAtif | last post by:
HI.. I am looking for php code that will enable me to hide/show a text box based on the users previous drop down list response. If javascript comes into use for it, its a welcome solution (1)...
0
1798
by: stirucherai | last post by:
Here is my what I am trying to do 1. Open a MP3 file (Sound) -- Multiple songs -- roughly 30 seconds per song 2. Dynamically create TEXT Field using CreateText method 3. Show Lyrics for the...
2
3244
by: s2008 | last post by:
Hello, In my page i have two textboxes and a html button. I'm using vb.net 2.0 . what i need is i want whatever text selected in the textbox1 to be hyperlinked when clicked the button... this is...
2
2333
by: Molham | last post by:
How to show same text as hyperlink to other new tab page? <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default2.aspx.vb" Inherits="Default2" %> <!DOCTYPE html PUBLIC "-//W3C//DTD...
0
7136
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7182
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7232
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6906
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7397
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4923
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3106
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1430
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
316
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.