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.