Saving styles for a blog - c#

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/)

Related

Include hyperlinks in description of phone call entity

My code generates a phone call activity in each customer lead, then records the SMS conversation between that customer and the company. The description may contain an URL to an image that the customer finds relevant.
I can put the URL as text in the description property, but I would like to transform it to a hyperlink (something like an <a> tag of html.) That way I can click to open it directly instead of copy pasting the URL first.
How can I achieve this?
The description field on a phone call is just a plain old text field, so you can't add any formatting or hyperlinks there.
You could make a separate field, single line of text, with a type of URL. Then the URL you input should act as a typical hyperlink.
The data types are documented at Create and Edit Fields.
I believe you're asking about being able to have a large text area, within CRM, that is editable, but allows you to enter, or at least click on, hyperlinks.
I see two supported solutions, but both would take a lot of customization.
Create an HTML webresource that loads the text from the field, parses it, looking for hyper links, and then add's the correct <a> tagging in order for the links to be clickable.
Search for a client side wiki markup Text Editor widget of some sort (possibly something like http://goessner.net/articles/wiky/ ?), and then format the hyper links with the correct markup.
None of CRM data types support native full blown html rendering. If you try to use JavaScript to update the value and try to render it as html, it would trigger a save to the database as CRM would see the attribute value as modified.

Best method or control to display text from a file in an asp.net webpage

This may be a totally newbie question, but here it goes. I have a asp.net web page that I need to display text from a .txt file. I am trying to figure what would be the best control to do this with or the best method. I looked at using an iframe, but this does a very poor job of displaying the text from the file (for instance no word wrap for an iframe). I don't really expect anyone to solve this for me completely, but if you have any suggestions or know of any links to tutorials or explanations where someone has done this, I would be very greatful.
Thanks
You can for example add a Literal control, assign File.ReadAllLines("yourfile.txt") to the Text property and replace \r\n with <br />.
You should just read the text-file in code (using a streamreader for example). Once you have that text, just output it to your web page.
If you're using web forms you could place a label and then set the text of that label.
If you're using MVC you could put it in the ViewBag and then in your view output the value from the ViewBag (or use a custom viewmodel)
You could use a Literal or Label control. Make sure that the control that you use encodes the text in order to avoid XSS vulnerabilities (or encode the text manually if necessary).
It might as well be necessary to substitute line endings with <br/> tags.

How to build custom sentences with OpenXML

I would like to know if there is a way to "play" with sentences in a .docx.
Here's what I need to do:
I have a paragraph in a document
exemple of my paragraph:
This is a paragraph that I need to format based on some conditions and I can't figure how to do this with openxml sdk.
end of exemple.
So based on a condition that I evaluate in c# I would like to add/remove the text. Another thing that you should know id that I would like the product owner to change the text of the document.
Basicly what I want to acheive is having a template document that my product owner can edit at will but in this document the text might change based on for whom the document is produce.
Thanks
You can create a template document in Word and create a content control in each place you want the text to be dynamically inserted. For each created content control, you should set unique Tag property value.
In your c# application code, you can then find a content control by its tag quite easily. After you find it, you can save its parent node, remove the content control and insert the text you want as a child paragraph element for the parent node saved earlier.
You can add content controls by using Developer tab. If you can't see it, you can use steps described here in order to show it: http://msdn.microsoft.com/en-us/library/bb608625.aspx .
There are some complexities involved in replacing text because text can be broken into multiple runs. If you want your owner to be able to edit the document, this is a problem you must solve. Luckily, there is sample code that you can use as part of the PowerTools for Open XML. PowerTools were written as cmdlets for PowerShell, but you can take the core C# code for your own programs. The TextReplacer.cs module should give you a good starting point. You will need some of the other modules that it depends on, like PtOpenXmlDocument.cs, PtOpenXmlUtil.cs and PtUtil.cs. I hope that helps.

highlight a word in textarea in ASP.NET using 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/

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