简体   繁体   English

asp.net文本框选择

[英]asp.net textbox selection

I have a standard asp:textbox which is editable and I want to be able to select a portion of text, click a button, and then encapsulate the selected text with html tags. 我有一个可编辑的标准asp:textbox,我希望能够选择一部分文本,单击一个按钮,然后用html标签封装所选文本。

For example the textbox has this text: 例如,文本框具有以下文本:

The quick brown fox jumps over the lazy dog. 敏捷的棕色狐狸跳过了懒狗。

If I select "The quick brown fox" and click a button I want the text to become <i>The quick brown fox</i> 如果我选择“快速的棕色狐狸”并单击一个按钮,我希望文本变为<i>The quick brown fox</i>

How to detect the selected text? 如何检测所选文字? With winforms you can use richtextbox but no such thing in standard asp.net controls. 使用winforms可以使用richtextbox,但在标准的asp.net控件中则不能使用。 And it also MUST be an asp:texbox control. 它也必须是一个asp:texbox控件。

You can use CKEditor as advanced html editor instead of textarea. 您可以将CKEditor用作高级html编辑器,而不是textarea。 http://ckeditor.com/ http://ckeditor.com/

Telerik has a very good rich textbox editor. Telerik有一个非常好的富文本框编辑器。 http://www.telerik.com/developer-productivity-tools.aspx http://www.telerik.com/developer-productivity-tools.aspx

I rarely recommend this, because the controls are kind of clunky, but you could use the ASP.NET AJAX Control Toolkit for this. 我很少建议这样做,因为控件有点笨拙,但是您可以为此使用ASP.NET AJAX控件工具包 Just follow the instructions on the website to install the toolkit, and include the DLL in your project. 只需按照网站上的说明安装工具箱,然后将DLL包含在您的项目中即可。

The toolkit has an extender that you can apply to a normal <asp:TextBox> called HTMLEditorExtender . 该工具包具有一个扩展程序,您可以将其应用于称为HTMLEditorExtender的常规<asp:TextBox> See the samples on the linked page for the functionality. 有关功能,请参见链接页面上的示例。 Basically, all you have to do is point the HTMLEditorExtender markup at an existing TextBox control. 基本上,您要做的就是将HTMLEditorExtender标记指向现有的TextBox控件。

Here's a simplified version from the sample on that page: 这是该页面上的示例的简化版本:

<ajaxToolkit:HtmlEditorExtender ID="HtmlEditorExtender1" 
    TargetControlID="yourTextBoxID" DisplaySourceTab="true" 
    runat="server"/>
    <Toolbar> 
        <ajaxToolkit:Bold />
        <ajaxToolkit:Italic />
        <ajaxToolkit:Underline />
        <ajaxToolkit:StrikeThrough />
        <ajaxToolkit:RemoveFormat />
        <ajaxToolkit:BackgroundColorSelector />
        <ajaxToolkit:ForeColorSelector />
        <ajaxToolkit:FontNameSelector />
        <ajaxToolkit:FontSizeSelector />
    </Toolbar>
</ajaxToolkit:HtmlEditorExtender>

Note that you need to set "yourTextBoxID" in TargetControlID="yourTextBoxID" to the ID of the TextBox control you want the extender to apply to. 请注意,您需要将TargetControlID="yourTextBoxID"中的TargetControlID="yourTextBoxID"设置为要应用扩展程序的TextBox控件的ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM