Avatar of D-bass
D-bass
 asked on

How to Create an On Key Press Event in Visual Studio 2005

Hi,

I have created a web form in visual Studio 2005. There is a comments text box which I have limited to 500 characters. I would like to display another textbox or label that shows how many characters remain until the max character limit is reached. Every time a character is entered I would like to have the textbox that is counting the characteres be updated. how can I do this using VB?

I am new to VB. Any help would be appreciated.

Thanks!
D-bass
Visual Basic.NETASP.NET

Avatar of undefined
Last Comment
D-bass

8/22/2022 - Mon
GreymanMSC

You do not.

All VB code is handled on the Server side.  This is processed only when a page is posted to the browser.  You do not want to postback every time you press a key.

You'll need to use Client side scripting to handle such activity; such as VBScript or Javascript.
<script type="text/javascript">
document.onkeyup = KeyCheck;       
function KeyCheck(e)
{
  var txt = document.getElementByID('<%= Me.EntryTextBox.ClientID %>;').getAttribute("value");
  document.getElementByID('<%= Me.CountTextBox.ClientID %>').setAttribute("value", txt.length);
}
</script>

Open in new window

D-bass

ASKER
I was not able to get this code to work. Here is what I have.

<script type="text/javascript">
document.onkeyup = KeyCheck;      
function KeyCheck(e)
{
  var txt = document.getElementByID('<%= Me.Comments.ClientID %>;').getAttribute("value");
  document.getElementByID('<%= Me.Textbox3.ClientID %>').setAttribute("value", txt.length);
}
</script>
Any sugestions?
ASKER CERTIFIED SOLUTION
D-bass

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy