highlight a word in textarea in ASP.NET using C# - c#

I have a TextArea control in my ASP.NET page which gets populated with a paragraph containing multiple sentences from the database. After this data gets populated in the TextArea control, I need to search for a few words in them and highlight them in different color. The words that I need to highlight are present inside a table in the database.
My question is : How do I highlight the selected words in a TextArea control using C#?
Please help. Thank you.

The HTML <textarea> tag doesn't contain the ability to add any text formatting. If you want to highlight a portion of your text, then you'll need to display it within a <div>, <span> or some other HTML element.
If you need to make the text editable and still highlight portions of it, then you could use a WYSIWYG HTML editor, such as the jHtmlArea Free, Open Source jQuery plugin.
Shameless Plug: jHtmlArea is a jQuery plugin I created a while back to fit a need I had for a light weight, easily extensible WYSIWYG HTML editor.

You can look into the DynaCloud jQuery plugin, or CodeMirror. Both provide some functionality for highlighting text.
http://johannburkard.de/blog/programming/javascript/highlight-javascript-text-higlighting-jquery-plugin.html
http://codemirror.net/

Related

Display html content in an ASP.NET Textbox

Its often that we need to display some formatted html text in a Web page. Here is what I want to do
There is a description field where user enters his comments.There's anther History field which shows some history about the user edits done. I just want to bold the User comments. Everything else in History Text box can be normal text.
Now I have two options here
Let the User bold his comments by using a third party editor like TINYMCE or CKEDITOR.
Let me bold the text after the user clicks submit button on the Page and render it in History control.
If we use Approach 1, It will be an Issue to show html content in History control which is a plain Text box.
Similarly in approach 2, we can't directly display formatted text in history control and storing htmltags in DB might be an issue.
Now, I am in a confusion which approach is right and how to achieve this.Kindly comment with your expert views and suggestions
Thanks

Saving styles for a blog

I am creating a blog and I am wondering how to allow an admin user to add styles to their post. For instance, if they would like to bold a few words in a paragraph or add an image in the middle of the paragraph.
I am using MVC 4 and entity framework. The plan is to have a text area for the user to create a post and save to the database. And also allow the user to do basic formatting to the text.
Thanks in advance.
You will want to implement a (possibly jQuery?) HTML WYSIWYG editor, such as Redactor
. There are many WYSIWYG plugins out there that are configurable and hook to your textarea.
First try to make a simple textarea that can save your text in your database then after that you can change that textarea and use some plug-ins like ckeditor (references: http://drupal.org/project/ckeditor, http://drupal.org/project/ckeditor), you may also like to use nicEdit which I'm also using right now (http://nicedit.com/), or you may want to use WYSIWYM Markdown Editor[wmd] (https://github.com/derobins/wmd, http://code.google.com/p/wmd/) like this one on stack overflow.. And another one Rich Text Editor [RTE] (demo is here: http://www.kevinroth.com/rte/demo.htm, main site is here: http://www.kevinroth.com/rte/)

displaying email body as html in c# textbox

I am imitating my email client on a asp page. I have a gridview that displays things like from,subject,attachments, and the body. The body is html. When I view it in the grid view and set htmlencode= 'false' i can see it correctly. However, I want to the display it inside a textbox, so it can edit and forward it or reply.
Any ideas?
Thanks,
Load the HTML into a rich text editor to enable to user to edit it - be aware that most rich text editors out there are not designed to create email safe HTML and they will require a substantial amount of tweaking if you are going to support more than the most basic formatting.
Some example rich text editors:
http://ajaxcontroltoolkit.codeplex.com/
http://www.freetextbox.com/
Look into using a WYSIWYG (What You See Is What You Get) text editor like Cute Editor
you need to use an HTML Editor / TextBox, we use DevExpress components, very high quality but not free, there are also alternatives like Telerik or Infragistics and also free alternatives... see here: Best Free available wysiwyg html editor
Try this...
You can-t display html in textbox but you can get the source code.I am using this:) click here
You can try dinamycly add html code to page:
<div runat="server" id="myDiv">
</div>
and then set its InnerHtml property from the code-behind:
myDiv.InnerHtml = "your html here";
but with this way you cannot edit it...
if you are using Devexpress Controls,
you can use ASPxHTMLEditor.
It works exactly as your requirement is.

Rich textbox that displays html like a web browser control

I am displaying text in a rich textbox and i want it to show the html formatting on the text. Is there a way to make a rich textbox display html.
If you push the button on the following link you will see how my out put displays, i would like it done in a rich textbox.
http://neil.fraser.name/software/diff_match_patch/svn/trunk/demos/demo_diff.html
Is there a way to build a rich textbox that can display html???
RTF encoding is different from HTML.
If not, then you need to write your own HTML to RTF converter or find something similar.
Writing Your Own RTF Converter. This guy gives a great breakdown of how the program works along with details of the conversion.
An Extended richtextbox control, Check this out. It may solve your solution

Safe HTML in ASP.NET Controls

Im sure this is a common question...
I want the user to be able to enter and format a description.
Right now I have a multiline textbox that they can enter plain text into. It would be nice if they could do a little html formatting. Is this something I am going to have to handle? Parse out the input and only validate if there are "safe" tags like <ul><li><b> etc?
I am saving this description in an SQL db. In order to display this HTML properly do I need to use a literal on the page and just dump it in the proper area or is there a better control for what I am doing?
Also, is there a free control like the one on SO for user input/minor editing?
Have a look at the AntiXSS library. The current release (3.1) has a method called GetSafeHtmlFragment, which can be used to do the kind of parsing you're talking about.
A Literal is probably the correct control for outputting this HTML, as the Literal just outputs what's put into it and lets the browser render any HTML. Labels will output all the markup including tags.
The AJax Control Toolkit has a text editor.
Also, is there a free control like the
one on SO for user input/minor
editing?
Stackoverflow uses the WMD control and markdown as explained here:
https://blog.stackoverflow.com/2008/09/what-was-stack-overflow-built-with/
You will need to check what tags are entered to avoid Cross side scripting attacks etc. You could use a regex to check that any tags are on a 'whitelist' you have and strip out any others.
You can check out this link for a list of rich text editors.
In addition to the other answers, you will need to set ValidateRequest="false" in the #Page directive of the page that contains the textbox. This turns off the standard ASP.NET validation that prevents HTML from being posted from a textbox. You should then use your own validation routine, such as the one #PhilPursglove mentions.

Categories