简体   繁体   中英

dynamic attribute value contain space C#

I am creating anchor tag dynamically and adding attributes in C#. adding data attributes like below

anchor.Attributes.Add("data-name", "[Sales][Experience About]");

what happened in UI it created like

<a data-name="[Sales][Experience" About] href=""></a>

because of spacing in attribute value ,the quotes closed in Experinece itself so it creating some problem for us.

how we can supply attriibute value with spaces in C#. we have so many attributes value we need to display like as like same value in UI too.

how to reslove this.

You'll need to use HttpUtility.UrlPathEncode to encode the attribute value:

anchor.Attributes.Add("data-name", HttpUtility.UrlPathEncode("[Sales][Experience About]"));

Which will result in

<a data-name="[Sales][Experience%20About]" href=""></a>

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