[英]Add to List from codebehind C# Asp.net
I have a UL list in a ASPX page: 我在ASPX页面中有一个UL列表:
<ul id="tabs">
<li id="tab1"><a href="ztab1.htm">Tab 1</a></li>
<li id="tab2"><a href="ztab2.htm">Tab 2</a></li>
<li id="tab3"><a href="ztab3.htm">Tab 3</a></li>
<li id="tab4"><a href="ztab4.htm">Tab 4</a></li>
</ul>
I would like to add list items dynamically from codebehind, including the href entry for each new list item. 我想从代码隐藏中动态添加列表项,包括每个新列表项的href条目。
How? 怎么样?
You need to mark your ul as a server side control, then treat the 'new item' as a HtmlGenericControl
and insert it into the control collection: 您需要将ul标记为服务器端控件,然后将“new item”视为
HtmlGenericControl
并将其插入到控件集合中:
<ul runat="server" id="tabs">
To add the item, create a new element and add it: 要添加项目,请创建一个新元素并添加它:
HtmlGenericControl li = new HtmlGenericControl("li");
tabs.Controls.Add(li);
HtmlGenericControl anchor = new HtmlGenericControl("a");
anchor.Attributes.Add("href", "page.htm");
anchor.InnerText = "TabX";
li.Controls.Add(anchor);
Use asp:bulletedList and your list will be much easier. 使用asp:bulletedList ,你的列表将更容易。
<asp:BulletedList id="blTabs"
BulletStyle="Disc"
DisplayMode="LinkButton"
runat="server">
<asp:ListItem Value="ztab1.htm">tab1</asp:ListItem>
<asp:ListItem Value="ztab2.htm">tab2</asp:ListItem>
<asp:ListItem Value="ztab3.htm">tab3</asp:ListItem>
</asp:BulletedList>
Code Behind: 代码背后:
ListItem li = new ListItem();
li.Value = "*.html"; //html goes here i.e. xtab1.html
li.Text = "New Text"; //text name goes i.e. here tab1
blTabs.Items.Add(li);
This result is the same as GenericTypeTea's, but the difference is the HTML is 'written' in the code behind and injected into the page. 此结果与GenericTypeTea相同,但不同之处在于HTML在后面的代码中“写入”并注入到页面中。
In your markup: 在你的标记中:
<asp:Literal id="litMarkup" runat="server" />
In your code behind: 在你的代码背后:
List<string> locations // however this initialized
StringBuilder sb = new StringBuilder();
sb.Append("<ul id=\"tabs\">");
for (int i = 0; i < locations.Count; i++)
{
sb.Append("<li id=\"tab" + i.ToString() + "\"><a href=\"" + locations[i] + "\">Tab " + i.ToString() + "</a></li>");
}
sb.Append("</ul>");
litMarkup.Text = sb.ToString();
You can create a dynamic UL by using an asp:Repeater
Control 您可以使用
asp:Repeater
Control创建动态UL
You can use repeater in following way, in your .aspx
file 您可以在
.aspx
文件中以下列方式使用转发器
<asp:Repeater ID="menu_ul_1" runat="server">
<HeaderTemplate>
<ul class="my-menu">
</HeaderTemplate>
<ItemTemplate>
<li>
<a href='<%# Eval("href_li")%>'>
<%# Eval("DisplayText")%></a>
</li>
</ItemTemplate>
<FooterTemplate>
</ul>
</FooterTemplate>
</asp:Repeater>
And you can put dynamic data via code behind in .aspx.cs
file 您可以通过代码将动态数据放在
.aspx.cs
文件中
protected void Page_Load(object sender, EventArgs e)
{
DataTable newsDataTable = new DataTable();
// add some columns to our datatable
newsDataTable.Columns.Add("href_li");
newsDataTable.Columns.Add("DisplayText");
for (int i = 1; i <= 5; i++)
{
DataRow newsDataRow = newsDataTable.NewRow();
newsDataRow["href_li"] = "?sc=item_" + i;
newsDataRow["DisplayText"] = "List Item # "+i;
newsDataTable.Rows.Add(newsDataRow);
}
menu_ul_1.DataSource = newsDataTable;
menu_ul_1.DataBind();
}
Result: You will get following html through this code 结果:您将通过此代码获得以下html
<ul class="my-menu">
<li><a href='?sc=item_1'>List Item # 1</a> </li>
<li><a href='?sc=item_2'>List Item # 2</a> </li>
<li><a href='?sc=item_3'>List Item # 3</a> </li>
<li><a href='?sc=item_4'>List Item # 4</a> </li>
<li><a href='?sc=item_5'>List Item # 5</a> </li>
</ul>
nested it with parent child,use your elements as below 使用父子项嵌套它,使用下面的元素
Dim newLi = New HtmlGenericControl("li")
Dim anchor = New HtmlGenericControl("a")
anchor.Attributes.Add("href", itemrow.Item("reg_url").ToString().Trim())
anchor.InnerText = itemrow.Item("conf_name")
newLi.Controls.Add(anchor)
eventList.Controls.Add(newLi)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.