1

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.

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>

How to detect the selected text? With winforms you can use richtextbox but no such thing in standard asp.net controls. And it also MUST be an asp:texbox control.

5
  • Why are you so certain about "must be asp:textbox"? I think that it's impossible. Commented Apr 10, 2013 at 19:08
  • Well it doesn't have to be I guess but it is part of the edit template of a gridview and not sure anything will work right in its place Commented Apr 10, 2013 at 19:10
  • Then search for some html rich text editors or try use contentEditable attribute for div. Commented Apr 10, 2013 at 19:12
  • I cannot bind to div unfortunately, only eval works, I get this error: A call to Bind must be assigned to a property of a control inside a template. I need to bind because I need the text from the div to be send to the DB Commented Apr 10, 2013 at 19:36
  • Minor nitpick: you should really surround with <em>, not <i> =) Commented Apr 10, 2013 at 20:14

3 Answers 3

1

You can use CKEditor as advanced html editor instead of textarea. http://ckeditor.com/

Sign up to request clarification or add additional context in comments.

Comments

0

I rarely recommend this, because the controls are kind of clunky, but you could use the ASP.NET AJAX Control Toolkit for this. Just follow the instructions on the website to install the toolkit, and include the DLL in your project.

The toolkit has an extender that you can apply to a normal <asp:TextBox> called HTMLEditorExtender. 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.

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.

Comments

0

Telerik has a very good rich textbox editor. http://www.telerik.com/developer-productivity-tools.aspx

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.