c# - randomize display order for controls in asp.net -
i know how can randomize order in controls displayed. basically, have 5 hyperlinks, , want on each load order in displayed randomized. way can accomplish that? thanks, laziale
assuming 5 links in asp panel named "pnllinks", , links controls in panel:
<asp:panel id="pnllinks" runat="server"> <asp:hyperlink id="hyperlink0" runat="server" text="test1" /> <asp:hyperlink id="hyperlink1" runat="server" text="test2" /> <asp:hyperlink id="hyperlink2" runat="server" text="test3" /> <asp:hyperlink id="hyperlink3" runat="server" text="test4" /> <asp:hyperlink id="hyperlink4" runat="server" text="test5" /> </asp:panel>
use code randomize order:
protected void page_load(object sender, eventargs e) { var ctrls = new list<control>(); foreach (control c in pnllinks.controls) { ctrls.add(c); } random rand = new random(); //randomize , put in foreach (control c in ctrls.orderby(c => rand.next())) { pnllinks.controls.remove(c); pnllinks.controls.add(c); } }
Comments
Post a Comment