Summary of three methods for batch operations on unordered lists by ASP. NET

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.