Loop through <li> with ASP.NET
I want to change the visibility of the <li> server side of the collections.
My HTML code looks like this:
<ul> <li runat=server id="l1"></li> <li runat=server id="l2"></li> <li runat=server id="l3"></li> <li runat=server id="l4"></li> ... </ul> Now that I said, I want to iterate over the collection and change the visibility of some li using my id
sort of
for (i=0;...) { l+I.visible=false } Any help would be appreciated.
+4
2 answers
Unused , but this may work:
First change, your ul will be running on the server.
<ul runat="server" id="myList"> Then iterate through it
foreach (Control li in myList.Controls) { if(li is HtmlGenericControl) li.Visible = false; } Code example:
Html:
<ul runat="server" id="myList"> <li runat=server id="l1">1</li> <li runat=server id="l2">2</li> <li runat=server id="l3">3</li> <li runat=server id="l4">4</li> </ul> <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" /> Click Handler Button:
protected void Button1_Click(object sender, EventArgs e) { foreach (Control li in myList.Controls) { if (li is HtmlGenericControl) li.Visible = false; } } EDIT 2 - Code in VB.NET
For Each li As Control In myList.Controls If TypeOf li Is HtmlGenericControl Then li.Visible = False End If Next +6