Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
<asp:TextBox ID=txtComments runat="server" BorderColor="Gray" 
           BorderStyle="Solid" BorderWidth="1px" CausesValidation="true" Wrap="true"
           MaxLength="100"
           TextMode="MultiLine" 
           Width="350px">


in aspx.cs file

public int MaxLength
   {
       get { return txtComments.MaxLength; }
       set { txtComments.MaxLength = value; }
   }
   public void wordCount()
   {
       txtCount.Text = txtComments.MaxLength.ToString();
       txtComments.Attributes.Add("onKeyUp", "javascript:document.getElementById('" + txtCount.ClientID + "').setAttribute('value', (" + txtComments.MaxLength + " - parseInt(document.getElementById('" + txtComments.ClientID + "').getAttribute('value').length)));");

   }



i was writing following code in my application.. ccan anyone tell me that how can i fixed the cursor at the end of last character in textbox...?

thank you in advance.

[edit]Code blocks added - OriginalGriff[/edit]
Posted
Updated 29-Apr-11 23:03pm
v2

1 solution

This always seems like it ought to be a good idea but make sure you really want this because it means the user can only ever use backspace or type which if they make a typo at some point and have to delete all of what they have already done it can be a pain. It also stops the user being able to highlight any of the text and copy it which is very unhelpful in a lot of cases. However,

If you realy want to do this you will need a short bit of javascript that fires on the following events:

JavaScript
focus()
onKeyUp() //(after all other code)
onMouseUp()


and to set the cursor to the end simply do:

this.value = this.value; in the javascript event as this will set the text (of the box) to itself thus forcing the cursor to be reset to the end.
 
Share this answer
 
Comments
Sandeep Mewara 30-Apr-11 6:01am    
My 5!
Ed Nutting 30-Apr-11 6:07am    
Thanks :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900