简体   繁体   中英

Enable HTML Tag inside DB Query String

I have a database query that displays a string:

db.query.record

"This is a string"

I want the string to be read:

"This
is
a
string"

the code i used was db.query.record.replace(" ", "<br />") and the results i got were:

"This <br> is <br> a <br> string"

What i understand is, the replace syntax is storing the html tag in the database string before it is read. After it is read, anything inside the string is displayed just as it is stored (as a string). I need the html to bypass the string, and fetch the html tag, and perform the action.

Suggestions, ideas? I've tried everything... and all results i've tried were done to the text string... and not to the actual html. Be mindful that i dont need this to affect the entire document, just the queried data. I need it to be multi-line, because the data is making my page disorganized (text vary in lengths and size)

If there are any answers that cover the topic Literals or LiteralsControl can you please include extra details, cause either i dont understand enough or it, or i was doing it wrong. Internet queries does not provide enough understanding for me.

I want to perform the complete opposite of this request: overflow-link

I know I've personally had trouble with this, but on a web page. So, I'm assuming you're trying to add content to your page.

If you're adding the text to, let's say, a <p> tag, you'll need to set the text via .innerHTML as opposed to .innerText .

Example:

HTML:

<p id='myP' runat='server'>Test</p>

VB:
myP.innerHTML = db.query.record.replace(" ", "<br />");
Or
myP.Controls.Add(New LiteralControl(db.query.record.replace(" ", "<br />")));

The answer was from another overflow question: .Replace(Environment.NewLine, "<br />") works on localhost but not when I upload my website to host

@Html.Raw(Html.Encode(Model.Body)
.Replace(Environment.NewLine, "<br />"))<br />

i simply changed out Model.Body with db.query.record and Environment.NewLine with " " . Apperently, that did the trick. What i discovered was that encoding/decoding the string was my issue, but it just wasnt applying at the right stage. It was supposed to be done a period after it was compiled, but before it was read (by the browser). I'm not sure, something like that... Anyways, i understand what i did wrong, and i figured it out. Thanks to those that submitted.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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