简体   繁体   中英

writing html tags & css in code behind C#

can somebody advise writing code the below way, is it a good practice?

this.ltlTtlRes.Text = string.Format("<span>{0}</span>", oComp.Emls.Count().ToString());

currently im reviewing code, and I am finding such html in code behind, not sure if it is a good practice, can somebody advise?

Thanks in advance..

For this stuff, the .Net library contains something called a HttpTextWriter , and comes with many stuff to nicely balance tags, escape content, etc.

http://revenmerchantservices.com/post/C-HtmlTextWriter-Example.aspx

Althought using this class is a little verbose, please think again about the possible issues with script injection or such. You can always write a little wrapper method.

PS.

oComp.Emls.Count().ToString()

Leave out the .ToString() . string.Format is very well capable to format the number with the {0} placeholder. Check string.Format formatting numbers.

http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx

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