This article introduces three methods for batch operation of unordered list based on native html tags by ASP. NET on the server.
Method 1: Make the li element into an html control, add the id, and use the FindControl method.
Aspx code:Copy codeThe Code is as follows: <form id = "form1" runat = "server">
<Ul>
<Li id = "li1" runat = "server"> Initial Value: 1 </li>
<Li id = "li2" runat = "server"> Initial Value: 2 </li>
<Li id = "li3" runat = "server"> Initial Value: 3 </li>
<Li id = "li4" runat = "server"> Initial Value: 4 </li>
<Li id = "li5" runat = "server"> Initial Value: 5 </li>
<Li id = "li6" runat = "server"> Initial Value: 6 </li>
<Li id = "li7" runat = "server"> Initial Value: 7 </li>
<Li id = "li8" runat = "server"> Initial Value: 8 </li>
</Ul>
<Asp: Button ID = "Button1" runat = "server" Text = "Button" onclick = "button#click"/>
</Form>
Aspx. cs code:Copy codeThe Code is as follows: protected void button#click (object sender, EventArgs e)
{
// Click the button to batch change the inline text value and style of the li Element
For (int I = 1; I <= 8; I ++)
{
HtmlGenericControl li = this. FindControl ("li" + I) as HtmlGenericControl;
Li. InnerHtml = "New Value" + I. ToString ();
Li. Attributes. CssStyle. Value = "color: red ";
}
}
Method 2: Make ul and li elements into html Controls and traverse them using the Controls set of ul Controls.
Aspx code:Copy codeThe Code is as follows: <form id = "form1" runat = "server">
<Ul id = "ul1" runat = "server">
<Li runat = "server"> Initial Value: 1 </li>
<Li runat = "server"> Initial Value: 2 </li>
<Li runat = "server"> Initial Value: 3 </li>
<Li runat = "server"> Initial Value 4 </li>
<Li runat = "server"> Initial Value: 5 </li>
<Li runat = "server"> Initial Value: 6 </li>
<Li runat = "server"> Initial Value: 7 </li>
<Li runat = "server"> Initial Value: 8 </li>
</Ul>
<Asp: Button ID = "Button1" runat = "server" Text = "Button" OnClick = "button#click"/>
</Form>
Aspx. cs code:Copy codeThe Code is as follows: private int counter = 1;
Protected void button#click (object sender, EventArgs e)
{
// Click the button to batch change the inline text value and style of the li Element
Foreach (Control control in ul1.Controls)
{
If (control is HtmlGenericControl)
{
HtmlGenericControl li = control as HtmlGenericControl;
Li. InnerHtml = "New Value" + (counter ++). ToString ();
Li. Attributes. CssStyle. Value = "color: red ";
}
}
}
Method 3: Use HtmlAgilityPack to operate elements in Dom mode.
Aspx code:Copy codeThe Code is as follows: <form id = "form1" runat = "server">
<Ul id = "ul1" runat = "server">
<Li> initial value 1 </li>
<Li> Initial Value 2 </li>
<Li> Initial Value 3 </li>
<Li> Initial Value 4 </li>
<Li> Initial Value 5 </li>
<Li> Initial Value 6 </li>
<Li> initial value 7 </li>
<Li> initial value 8 </li>
</Ul>
<Asp: Button ID = "Button1" runat = "server" Text = "Button" OnClick = "button#click"/>
<Asp: Button ID = "Button2" runat = "server" Text = "test empty resend"/>
</Form>
Aspx. cs code:Copy codeThe Code is as follows: protected void button#click (object sender, EventArgs e)
{
// Click the button to batch change the inline text value and style of the li Element
HtmlDocument htmlDoc = new HtmlDocument ();
HtmlDoc. LoadHtml (ul1.InnerHtml );
HtmlNodeCollection lis = htmlDoc. DocumentNode. SelectNodes ("li ");
For (int I = 0; I <lis. Count; I ++)
{
Lis [I]. InnerHtml = "New Value" + (I + 1). ToString ();
Lis [I]. Attributes. Add ("style", "color: red ");
}
Ul1.InnerHtml = htmlDoc. DocumentNode. InnerHtml;
}
The above three methods have their own advantages and disadvantages. You can select them based on your actual situation.