Link to home
Create AccountLog in
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
Avatar of GreymanMSC
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

Avatar of 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
Avatar of D-bass
D-bass

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer