I am trying to insert some text in to textbox in WebForm ASP.net. My goal is when the user select item from the dropdown the item will be added/inserted into the textbox text at the cursor position. I have tried different way but no luck. The issue I am facing is that I am not able to find the cursor position.
I have the textbox and dropdown list:
<div class="row" style="text-align: right; align-content: baseline">
<div class="col-sm-12 col-md-12 col-lg-12 margin_top_bottom_10px" style="display:flex">
<asp:DropDownList ID="ddlElementToAdd" runat="server" Width="20%" />
</div>
<div class="col-sm-12 col-md-12 col-lg-12 margin_top_bottom_10px">
<asp:TextBox ID="TextBox1" runat="server" Width="100%" Height="500"
TextMode="MultiLine"></asp:TextBox>
</div>
</div>
I add the item to dropdown list onload the page.
Any suggestion
Thanks
2
Answers
When you select a drop down then the cusor focus is moved to drop down. As the focus is not at the text box there is no way you can get the cursor position of the text box as technically the focus is at dropdown
If you want, you can add the text at the end of the texbox or at the specific position in textbox using drop down onchange event where you can call a function to add the text in the texbox using some string functions
Well, you can do this. But it will work like this:
Say we have two text boxes. We can click on the text box – type in, cursor around.
then you want to save typing, so you now go to the combo box, and select a value, and it will insert the combo box into your text and last cursor postion.
What this will require is when you click on the text box OR YOU move around, we need to grab both the click event (save cursor postion), or editing (again, save cursor postion).
This markup works – messey and quick + dirty – but it does work and it works against two edit text boxes.
The two fields I have of course would (should) be hidden with style=display:none.
But, this code does work:
And so it looks like this:
So, you can now click on, or type + edit either text box, and if you go up to the combo box, select a choice – the text is inserted at your current position. It works for either text box.