-3

If it was Windows Forms, I could have used

textbox1.Select(textbox1.Text.Length,0);

How can I achieve this in C#, for an ASP.Net Textbox. I would like to do this only in C#

Ruby
  • 949
  • 7
  • 31
  • 68
  • If you can get c# running in the client-browser you earn a medal. You can't from c# alone – lboshuizen Nov 03 '13 at 14:19
  • And no, you can't do this in ONLY c#. A text box is a client-side object, c# is server side. – gunr2171 Nov 03 '13 at 14:19
  • How come, we have option to set focus from server side, but not place a cursor. I guess, JS is the only way. Its just that, for the life of me, am unable to send the textbox ID to the js function.http://stackoverflow.com/questions/19543075/send-asp-net-control-id-to-a-javascript-function – Ruby Nov 03 '13 at 14:43

3 Answers3

1

You must register appropriate javascript script to run on the page load (or any other way to run javascript). You can not achieve such behaviour on server side.

Community
  • 1
  • 1
Agat
  • 4,577
  • 2
  • 34
  • 62
0

This one worked for me (works in chrome and IE)

<asp:TextBox ID="txtBranch" runat="server" onfocus="this.value=this.value;">
</asp:TextBox>

Courtesy: Use JavaScript to place cursor at end of text in text input element

Community
  • 1
  • 1
Ruby
  • 949
  • 7
  • 31
  • 68
0

If I understand your question correctly, you can call Focus method from code behind.

It basically places a cursor inside TextBox using JavaScript, when the page is loated at client side.

<asp:TextBox runat="server" ID="textbox1"></asp:TextBox>

protected void Page_Load(object sender, EventArgs e)
{
    textbox1.Focus();
}
Win
  • 61,100
  • 13
  • 102
  • 181