Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Set focus and cursor at end of text?

Reply
Thread Tools

Set focus and cursor at end of text?

 
 
Olav Tollefsen
Guest
Posts: n/a
 
      01-19-2004
I use this code to set focus to a textbox when I load a page:

private void SetFocus(System.Web.UI.Control ctrl)
{
string s = "<SCRIPT language=\"javascript\">document.getElementById('" +
ctrl.ID + "').focus()</SCRIPT>";

RegisterStartupScript("focus", s);
}

I would also like the cursor to be positioned at the end of the text field.
Using only the above code, the cursor is positioned at the beginning of the
textbox even if there are some text present in the field.

How can I position the cusrsor at the end of the text?

Olav


 
Reply With Quote
 
 
 
 
Mohammad A. Samara
Guest
Posts: n/a
 
      01-20-2004
Olav,

Here we go:

Create a new function in your <head> tag in the HTML section:

function SetEnd (TB)
{
if (TB.createTextRange)
{
var FieldRange = TB.createTextRange();
FieldRange.moveStart('character', TB.value.length);
FieldRange.collapse();
FieldRange.select();
}
}

Create a new onfocus JavaScript event in your asp text box tag (asp:textBox)
that calls the SetEnd function above:

<asp:textbox id="TextBox1" runat="server" Width="65px"
onfocus="SetEnd(this)">Existing text</asp:textbox>

Make sure you keep your existing code that calls the SetFocus function as it
is:

private void SetFocus(System.Web.UI.Control ctrl)
{
string s = "<SCRIPT language=\"javascript\">document.getElementById('" +
ctrl.ID + "').focus()</SCRIPT>";

RegisterStartupScript("focus", s);
}

Don't forget to call the above SetFocus() function in your Body tag on page
load .....( <body onload="SetFocus()"> )

Good luck!

Regards,

Mohammad Samara.

ICS (London) Ltd.


"Olav Tollefsen" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> I use this code to set focus to a textbox when I load a page:
>
> private void SetFocus(System.Web.UI.Control ctrl)
> {
> string s = "<SCRIPT language=\"javascript\">document.getElementById('" +
> ctrl.ID + "').focus()</SCRIPT>";
>
> RegisterStartupScript("focus", s);
> }
>
> I would also like the cursor to be positioned at the end of the text

field.
> Using only the above code, the cursor is positioned at the beginning of

the
> textbox even if there are some text present in the field.
>
> How can I position the cusrsor at the end of the text?
>
> Olav
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to tell if cursor is sqlite.Cursor or psycopg2.Cursor dmaziuk Python 3 01-25-2011 04:52 AM
How to set the cursor to the end of a input box? Cylix Javascript 1 07-09-2007 10:45 AM
this.window.focus() vs. window.focus() vs. this.focus() Roger Javascript 3 03-08-2007 08:53 PM
Set cursor position to the end of a text field ? duzuike Javascript 2 08-11-2005 09:28 AM
Set cursor in first text box and set button as default SibAndela ASP General 1 09-26-2004 06:31 PM



Advertisments