在asp.net文本框中使用选定的文本

时间:2023-01-10 00:54:09

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?

我有一个文本框,里面有常规文本。我希望用户能够选择一个单词并从该单词创建超链接。我将通过基本上使用该单词并将其替换为标记并添加网址信息来完成此操作。我的问题是如何从c#代码中获取用户选择的文本。我看到了一些关于selecttext的东西,但这只适用于windows窗体文本框。有任何想法吗?

thanks,

谢谢,

3 个解决方案

#1


1  

I suggest using javascript.

我建议使用javascript。

var startPos =input.selectionStart;
var endPos = input.selectionEnd;
var selectedText = input.value.substring(startPos, endPos);

#2


1  

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

这在纯asp.net中是不可能的。哟必须添加一些客户端技术来拾取浏览器中的动作。

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

使用常见的html页面,查看Jquery或其他javascript框架以及突出显示的文本,例如:http://mark.koli.ch/2009/09/use-javascript-and-jquery-to-get-user-选择-text.html

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

或者看一看silverlight,你可能会发现你可以使用windows窗体的所有工具。

#3


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.

你不能从代码背后做到这一点,因为你的C#代码在服务器上运行,用户在客户端上选择这个文本。你将不得不使用Javascript。

I reccomend jQuery FieldsSelection :

我推荐jQuery FieldsSelection:

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

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

#1


1  

I suggest using javascript.

我建议使用javascript。

var startPos =input.selectionStart;
var endPos = input.selectionEnd;
var selectedText = input.value.substring(startPos, endPos);

#2


1  

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

这在纯asp.net中是不可能的。哟必须添加一些客户端技术来拾取浏览器中的动作。

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

使用常见的html页面,查看Jquery或其他javascript框架以及突出显示的文本,例如:http://mark.koli.ch/2009/09/use-javascript-and-jquery-to-get-user-选择-text.html

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

或者看一看silverlight,你可能会发现你可以使用windows窗体的所有工具。

#3


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.

你不能从代码背后做到这一点,因为你的C#代码在服务器上运行,用户在客户端上选择这个文本。你将不得不使用Javascript。

I reccomend jQuery FieldsSelection :

我推荐jQuery FieldsSelection:

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

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