Solved

Inserting text into textbox at cursor position

Posted on 2009-05-13
4
1,076 Views
Last Modified: 2013-11-08
In the simplest terms I have a button and a multiline textbox on my asp.net page. I type data into the textbox and when I click a button I need it to insert text at the cursor position.

This is also within an updatepanel. Is this possible?
0
Comment
Question by:syinner
  • 3
4 Comments
 
LVL 10

Expert Comment

by:MaxOvrdrv2
ID: 24374787
can you use client-side script (JavaScript) or you are limited to server-side?
0
 

Author Comment

by:syinner
ID: 24374838
i got no problem using client side however this is within a control that is part of a content page... i have had problems with client side javascript when this is combined with an updatepanel.

i would like the server side to make the decision what needs to inserted through.
0
 
LVL 10

Accepted Solution

by:
MaxOvrdrv2 earned 500 total points
ID: 24374912
the problem is that you cannot get the current cursor location from a textbox using server-side code... unless you are certain that the location will be either the beginning, or end of the text entered...  here's the JavaScript way of inserting text at the cursor location... this also gives you a selected text if you need it... use @pos@ var for current position:
function InsertAtLocation()

{

  var ctrl = 

 document.getElementById("<%=Me.TextBoxName.ClientID%>");

  var saveText = ctrl.value;

  ctrl.focus();

  var range = document.selection.createRange();

  var specialchar = String.fromCharCode(1);

  range.text = specialchar;

  var pos = ctrl.value.indexOf(specialchar);

  ctrl.value = saveText;

  range = ctrl.createTextRange();

  range.move('character', pos);

  range.select();

  range.text = "INSERT YOUR TEXT HERE, USING <%=%> IF NEEDED"

  document.getElementById("txtTargetText").focus();

  window.event.returnValue = false;

}

Open in new window

0
 
LVL 10

Expert Comment

by:MaxOvrdrv2
ID: 24374921
something like that anyway... for mine here i'm copying text from one text box to another... but you get the idea from the code anyway.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now