Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to use style tags inside a label? 2

Status
Not open for further replies.

autumnEND

Programmer
Nov 1, 2005
61
GB
Im using a label to display articles posted by users on my site.

if the user enters <strong> word </strong>

im wanting it to convert the text around the strong tags to bold text.

but i cant seem to find a way to do it.

is there anyway to do this? any help would be appreciated

thanks

heres the code for the label
__________________________________

<asp:Label ID="newsARTICLELabel" runat="server" Text='<%# Replace(Server.HTMLEncode(Convert.ToString(Eval("newsARTICLE"))),vbcrlf,"<br>") %>' Width="96%"></asp:Label>

 
Label tags should render <strong> tags for you automatically...


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
maybe it doesnt alter it automatically becasue i convert the label value to string and also becuase i make it so it alters line breaks

vbcrlf,"<br>")

but im not sure !

 
After you HtmlEncode the label text, <strong> will appear as: &lt;strong&gt;. A simple, albeit inelegant way to fix your problem would be to run a replace after HtmlEncode that seeks out and changes &lt;strong&gt; back to <strong> and do something similar for the closing tag.
 
how would i go about doing that?
im pretty new to this.
heres what i tried

<asp:Label ID="newslabel" runat="server" Text='<%# Replace(Server.HTMLEncode(Convert.ToString(Eval("newsARTICLE"))),vbcrlf,"<br>",&lt;strong&gt,"<strong>",&lt;strong&gt,"</strong>") %>' Width="96%"></asp:Label>

that didnt work.
the error was name 'strong' is not declared
and expression expected

could someone point me in the right direction please .
 
You'd have to wrap the Replace for the strong tag around the replace for the HTMLEncode e.g.
Code:
Replace(Replace(Server.HTMLEncode(Convert.ToString(Eval("newsARTICLE"))),"&lt;strong&gt;","<strong>")
You'd then have to do the same for the closing strong tag.

Is there a particular reason why you are setting the value of a label in this manner i.e. why aren't you setting it in code via "Label1.Text = myVariable" and also, for what reasons are you using the HTMLEncode function.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Is there a particular reason why you are setting the value of a label in this manner i.e. why aren't you setting it in code via "Label1.Text = myVariable" and also, for what reasons are you using the HTMLEncode function

im using HTMLEncode because at the time i couldnt find any other way to do it ..

im new to this...and just used the way i could to get it to work. without thinking about better solutions to the problem


i thought HTMLEncode was required to make the replace line brake with <br> display.

what would be a better way rather than using the HTMLEncode feature?
 
what would be a better way rather than using the HTMLEncode feature?
The way that I described above...


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
HtmlEncode will render any text sent to the browswer as harmless. It's good for security reasons because a user might enter something like:

Code:
<script type='text/javascript'>
       alert('security risk');
       //do something bad with cross site scripting
</script>

...or enter in other tags that end up breaking the page layout or something (like closing a table cell or a div where you don't want it to happen).

As you can see, however, the script I entered above didn't run. That's because (if you look at this page's source) you'll see that angle brackets are sent to the browser in their HTML character encodings: &lt; and &gt;. That's what HtmlEncode can do.

However, if you don't care about that stuff, you can, as ca8msm suggested, just not use it and all of the <strong> and even the <br> tags will just get spit to the browswer such that they are actually <strong> and <br> tags (instead of &lt;strong&gt; and &lt;br&gt;) when the page is delivered.

 
Thank you both for your help

i never meant HTMLEncode was a bad way of working

im just new to this, and wondered about other ways of dealing with the issue

ill get to work and try all the ideas mentioned :)

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top