c# - Confusing creating HTML List base on array -
i has problem creating html-list base on array in c#. tried using split.string, foreach, , etc,. still can't figured out logic... :(..
can me solve problem ?
here array
list<string> listmenu = new list<string>(); listmenu.add("dashboard~view1"); listmenu.add("dashboard~view2"); listmenu.add("customer"); listmenu.add("part"); listmenu.add("part~part1~part11"); listmenu.add("part~part1~part12"); listmenu.add("part~part2~part21"); listmenu.add("part~part2~part22"); listmenu.add("part~part3~part31~part311"); listmenu.add("part~part3~part31~part312"); listmenu.add("branch");
and want create html list :
<div id=menu> <ul> <li>dahboard <ul> <li> view1 </li> <li> view2 </li> </ul> </li> <li> customer </li> <li> part <ul> <li> part1 <ul> <li> part11 </li> <li> part12 </li> </ul> </li> <li> part2 <ul> <li> part21 </li> <li> part22 </li> </ul> </li> <li> part3 <ul> <li> part31 <ul> <li> part 311 </li> </ul> </li> <li> part 312 </li> </ul> </li> </ul> </li> <li> branch </li> </ul> </div>
there 2 ways can use create list dynamically:
you can use htmlgenericcontrol structure below:
htmlgenericcontrol c = new htmlgenericcontrol("div"); c.attributes.add("id", "menu"); htmlgenericcontrol ul1 = new htmlgenericcontrol("ul"); c.controls.add(ul1); htmlgenericcontrol li1 = new htmlgenericcontrol("li"); li1.innertext = "dashboard"; ul1.controls.add(li1); maindiv.controls.add(c);
you can create string stringbuilder , assign string innerhtml main div, below:
stringbuilder s = new stringbuilder(); s.append(@"<div id=menu> <ul> <li>dahboard <ul> <li> view1 </li> <li> view2 </li> </ul> </li> </ul> </div> "); //you can alter string dynamically maindiv.innerhtml = s.tostring();
you should add system.web.ui.htmlcontrols using use htmlcontrols
you should add system.text using in order use stringbuilder.
bonus: can find here why should use stringbuilder instead of adding string '+'.
Comments
Post a Comment