Asp. NET three methods for batch operation of unordered list _ practical skills

Source: Internet
Author: User
This article describes three methods of server-side asp.net bulk operations based on unordered lists of native HTML tags.
Method One, the Li element is made into HTML control, plus ID, using FindControl method.

ASPX code:
Copy Code code 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= "Button1_Click"/>
</form>

Aspx.cs Code:
Copy Code code as follows:

protected void Button1_Click (object sender, EventArgs e)
{
Change the inline text values and styles of LI elements in bulk after clicking the button
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 Two, will be UL, Li elements into HTML control, with the UL control Controls collection traversal.

ASPX code:
Copy Code code 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= "Button1_Click"/>
</form>

Aspx.cs Code:
Copy Code code as follows:

private int counter = 1;
protected void Button1_Click (object sender, EventArgs e)
{
Change the inline text values and styles of LI elements in bulk after clicking the button
foreach 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 III, using Htmlagilitypack, to manipulate elements in a dom manner.

ASPX code:
Copy Code code 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= "Button1_Click"/>
<asp:button id= "Button2" runat= "Server" text= "test null postback"/>
</form>

Aspx.cs Code:
Copy Code code as follows:

protected void Button1_Click (object sender, EventArgs e)
{
Change the inline text values and styles of LI elements in bulk after clicking the button
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;
}

Each of the above three methods have advantages and disadvantages, can be selected according to the 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.