Execute html code for a C# variable - c#

Let me explain in detail
There is textarea in form.
On submitting form,the text of textarea get stored in database.
While display those texts from database I want to execute basic html code like <b><i><u>...etc
For example:-If user entered "<b>hello</b>" in textarea.Then this word "hello" will display in bold letters.

Use #Html.Raw( [your HTML code from the database] ) in your View.

In your Razor-file:
<b>#myHtmlFromTheDatabase</b>

Related

to show html table from sql to asp.net mvc

i have problem in showing my data field that already in html code inside sql. need to show it on asp.net mvc.
below is the command of html in asp.net which i already add html as fieldtype so when it read html or textbox it will show the page in html format. but now for table, i cant properly show the exact table, but only the code.
enter image description here
the sqldata field action details in html table
enter image description here
this is the output, which is not show the exact table
enter image description here
help me out!
#Htm.Raw()
when you want to render html code stored in sql first you need html tag in ef to actually be able to save it from user end then you are subject of milion of hacking attempt which btw all of them fails and then when you want to reder html what you do is just render it with razor which is #Html.Raw(_yourContext.YourTable.FirstOrDefault(q=>q.yourPK == yourVariable).yourHtmlField)

CKEditor.Text is giving HTMLEncoded Output in ASP.NET

I am trying to use CKEditor into my ASP.NET Application. I got a good resource as follows
http://www.codeproject.com/Tips/532164/How-to-Integrate-CKEditor-with-ASP-NET
http://www.codeproject.com/Tips/455129/Rich-Text-Editor-with-ASP-NET
I want to set the CKEditor Text without losing its formatting (bold, Italics etc..) into a multiline Textbox. So, I am trying the following code.
string str = this.CKEditor1.Text;
TextBox1.Text = str;
Thus its giving me a html encoded Output as follows
<p>dfgdfgfdgdfgdfgdf<strong>gdf</strong></p>
But I don't want to have those tags around but formatting should be preserved. I tried using HTMLEncode and HTMLDecode, Also used this.CKEditor1.HtmlEncodeOutput = false; but of no avail.
Is there any other way I can save the text as it is without losing formatting into my Textbox?
I know textboxes are not meant for storing formatted html outputs but I have to store this(Comments) along with a formatting in a text box(for History) in my Application. Previously they were using Plain text boxes for both comments and History. Now richtext editing is needed and hence we are tying to go this way. Any other good approaches and suggestions are most welcome.
No, a Textbox cant display formatted values. That is why you have the CKEditor.
You can still store the html formated values in the database and display them (formatted) in for example a Label

ASP.NET saving text to database in correct format

I have an ASP.net 4.5 text box with 5 rows. When I save the below data to my table it does not take in consideration that there are line breaks.
Example:
Hello my name is Mike!
How are you?
Please can you help?
When saving this data to my table it of course save it like this below and it will be displayed like this again if I populate my website with this value.
Hello my name is Mike!How are you?Please can you help?
But I want it to be displayed with the line breaks as the way it was written in the first place.
The thing is, I do not want to make use of a custom form item because then the user will be able to copy and paste all html from another website in and I do not want that.
Any idea how this can be done without using a plugin such as below where the user will be able to copy and paste data into?
To populate your text box from the database try;
TextBox1.Text = someText.Replace("\r\n", Environment.NewLine)
For Label
Label1.Text = someText.Replace("\r\n", <br />)
Alternatively you could save the data to your database with the
someText = TextBox1.Text.Replace("\r\n", "<br />")
Although this approach may not be appropriate if the database is also used elsewhere.

How to bind html to an rdlc file

I have text stored in a column of a table within my database, with html tags .
Example :
<table><tr><td> hello </td></tr></table>
I am binding the text to the website and it works perfectly. It gets displayed as
hello
on the website.
Now I would like to create a rdlc report and am binding the same dataset to the report.
But I see :
<table><tr><td> hello </td></tr></table>
on the report instead of
hello
I think I understand why this is happenning , because the rdlc file cannot render html. But is there a way
that I can see "hello" on the website. ? Please let me know if you know of any other way to do it .
you can render using HTML tags in RDL as follows :
Left click the field you want to display so that the <Expr> tag is highlighted
Right click the highlighted <Expr> tag and choose Placeholder Properties...
On the General tab, select the HTML- Interpret HTML tags as style radio button

Insert html tag to a Database as a String and retrieve it back in C#

I want to insert html tag to a Database as a String and retrieve it back. after retrieved it ,I want to bind it in Grid.
as a example
String word = "<h1>This is Heading </h1> \n <h2>This is body</h2> \t This is after tab";
after I bind to the grid it should be
This is Heading(in a big font size)
This is body(in small font) - (tab space) This after tab
However this way is not working. It shows
<h1>This is Heading </h1> \n <h2>This is body</h2> \t This is after tab
word instead of applying real HTML behavior. I tryout with '\' special character removal but it remain same result.
Please help me.
Exactly which control are you using? Most of them will escape html like that for you, and to get it to be written 'as-is' you have to set a property.
If you're using GridView, then you can set the HtmlEncode on your BoundColumn to false.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.boundfield.htmlencode.aspx
If you're using AutoGenerateColumns, see this SO thread: Prevent HTML encoding in auto-generated GridView columns
Saving into DB is not a problem, you can save directly the string into DB,But when displaying on UI you must use HttpUyility.HtmlEncode(), else it will invite Javascript Hacking.
But if you are entering data from WebPage, make sure Input Validations are turned off Else you will get XSS script attack error.
you can bind your text in literal control it will represent the actual HTML formatted text.
like :
ltrlMsg.Text="<div class=\"Message Success\"> Your Message has been sent successfully! </div>"
or you can bind in you grid too.

Categories

Resources