c# - How to add value-less HTML5 attributes from code-behind -
this follow of question: how add <audio> tag content of page in code behind?
here creating audio control this.
var myaudio = new system.web.ui.htmlcontrols.htmlaudio(); myaudio.attributes.add("autoplay", "autoplay"); myaudio.attributes.add("controls", "controls"); myaudio.src = "test.mp3"; form.controls.add(myaudio); this renders in page as
<audio src="test.mp3" autoplay="autoplay" controls="controls"></audio> it works. ideally, rendered
<audio src="test.mp3" autoplay controls></audio> that is, valueless attribute keys autoplay , controls.
i tried
myaudio.attributes.add("autoplay", ""); //autoplay = ""; myaudio.attributes.add("autoplay", null); //autoplay attribute missing but these didn't yield satisfactory results.
is there way render control valueless attributes?
a possible solution override renderattributes method on inherited control.
public class mycustomaudio : system.web.ui.htmlcontrols.htmlaudio { protected override void renderattributes(htmltextwriter writer) { foreach (string key in this.attributes.keys) { string val = this.attributes[key]; writer.write(val == key ? string.format(" {0}", key) : string.format(" {0}=\"{1}\"", key, val)); } } } with same code:
var myaudio = new mycustomaudio(); myaudio.attributes.add("autoplay", "autoplay"); myaudio.attributes.add("controls", "controls"); myaudio.src = "test.mp3"; form.controls.add(myaudio); i got output:
<audio autoplay controls src="test.mp3"></audio>
Comments
Post a Comment