ASP.NET對無序列表大量操作的三種方法小結

來源:互聯網
上載者:User

本篇介紹伺服器端ASP.NET大量操作基於原生html標籤的無序列表的三種方法。
方法一,將li元素做成html控制項,加上id,用FindControl方法。

aspx代碼: 複製代碼 代碼如下:<form id="form1" runat="server">
<ul>
<li id="li1" runat="server">初始值1</li>
<li id="li2" runat="server">初始值2</li>
<li id="li3" runat="server">初始值3</li>
<li id="li4" runat="server">初始值4</li>
<li id="li5" runat="server">初始值5</li>
<li id="li6" runat="server">初始值6</li>
<li id="li7" runat="server">初始值7</li>
<li id="li8" runat="server">初始值8</li>
</ul>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
</form>

aspx.cs代碼: 複製代碼 代碼如下:protected void Button1_Click(object sender, EventArgs e)
{
//單擊按鈕後批量改變li元素的內聯文本值及樣式
for (int i = 1; i <= 8; i++)
{
HtmlGenericControl li = this.FindControl("li" + i) as HtmlGenericControl;
li.InnerHtml = "新值" + i.ToString();
li.Attributes.CssStyle.Value = "color:red";
}
}

方法二,將ul、li元素做成html控制項,用ul控制項的Controls集合遍曆。

aspx代碼: 複製代碼 代碼如下:<form id="form1" runat="server">
<ul id="ul1" runat="server">
<li runat="server">初始值1</li>
<li runat="server">初始值2</li>
<li runat="server">初始值3</li>
<li runat="server">初始值4</li>
<li runat="server">初始值5</li>
<li runat="server">初始值6</li>
<li runat="server">初始值7</li>
<li runat="server">初始值8</li>
</ul>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
</form>

aspx.cs代碼: 複製代碼 代碼如下:private int counter = 1;
protected void Button1_Click(object sender, EventArgs e)
{
//單擊按鈕後批量改變li元素的內聯文本值及樣式
foreach (Control control in ul1.Controls)
{
if (control is HtmlGenericControl)
{
HtmlGenericControl li = control as HtmlGenericControl;
li.InnerHtml = "新值" + (counter++).ToString();
li.Attributes.CssStyle.Value = "color:red";
}
}
}

方法三,利用HtmlAgilityPack,對元素以Dom方式操作。

aspx代碼: 複製代碼 代碼如下:<form id="form1" runat="server">
<ul id="ul1" runat="server">
<li>初始值1</li>
<li>初始值2</li>
<li>初始值3</li>
<li>初始值4</li>
<li>初始值5</li>
<li>初始值6</li>
<li>初始值7</li>
<li>初始值8</li>
</ul>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
<asp:Button ID="Button2" runat="server" Text="測試空回傳" />
</form>

aspx.cs代碼: 複製代碼 代碼如下:protected void Button1_Click(object sender, EventArgs e)
{
//單擊按鈕後批量改變li元素的內聯文本值及樣式
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 = "新值" + (i + 1).ToString();
lis[i].Attributes.Add("style", "color:red");
}
ul1.InnerHtml = htmlDoc.DocumentNode.InnerHtml;
}

以上三種方法各有優缺點,可根據實際情況選用。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.