0

I have a textbox that has regular text in it. I want a user to be able to select a word and create a hyperlink from that word. I will be doing this by basically using that word and replacing it with an a tag and adding url info. My question is how can I get the users selected text from c# code behind. I saw something about selectedtext but that only works for windows forms textbox. Any ideas?

thanks,

0

3 Answers 3

1

I suggest using javascript.

var startPos =input.selectionStart;
var endPos = input.selectionEnd;
var selectedText = input.value.substring(startPos, endPos);
Sign up to request clarification or add additional context in comments.

Comments

1

This is not possible in pure asp.net. Yo have to add some client technology to pickup the action made in the browser.

With common html page, have a look a Jquery, or another javascript framework, and highlighted text , eg:http://mark.koli.ch/2009/09/use-javascript-and-jquery-to-get-user-selected-text.html

or have a peek at silverlight, you might find there all the tools you can use with windows form.

1 Comment

Adding to Arthis answer, inside the function that selects the text you can then pass that text around. Two options I see, either an ajax call to send the selected text to the server, or you can create an asp:hiddenfield and set it's value which you can then retrieve when you postback.
0

You can't do that from code behind, becouse your C# code runs on server and user select this text on client. You will have to use Javascript.

I reccomend jQuery FieldsSelection :

https://github.com/localhost/jquery-fieldselection

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.