Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1103
  • Last Modified:

Inserting text into textbox at cursor position

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
syinner
Asked:
syinner
  • 3
1 Solution
 
MaxOvrdrv2Commented:
can you use client-side script (JavaScript) or you are limited to server-side?
0
 
syinnerAuthor Commented:
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
 
MaxOvrdrv2Commented:
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
 
MaxOvrdrv2Commented:
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now