looping over <li> using ASP.NET
I want to change the visibility of <li>
collection throw server side.
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 as I said I want to loop over the c开发者_开发百科ollection and change the visibility of some li using their ID
something like
for (i=0;...) {
l+I.visible=false
}
Any help will be grateful.
Untested, but this could work:
First change, your ul
to 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;
}
Example Code:
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" />
Button Click Handler:
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
Try this :
<ul>
<% for (int i = 0; i <= 10; i++){%>
<li runat=server id="l<%Response.Write(i); %>"><%=i%></li>
<% } %>
</ul>
just inside html page !
精彩评论