C# 如何在asp.net中动态生成无序列表?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10578824/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to generate unordered list dynamically in asp.net?
提问by Jelo Melo
I want to generate a unordered list which contains tag for showing images in my database, I take a look at bulleted list, but it is not working with image. How can i dynamically generate it from the results in a database, for eg, if i have 6 images in data base then i want the list which generates must look like this.
我想生成一个无序列表,其中包含用于在我的数据库中显示图像的标签,我查看了项目符号列表,但它不适用于图像。我如何从数据库中的结果动态生成它,例如,如果我在数据库中有 6 个图像,那么我希望生成的列表必须如下所示。
<ul id="">
<li><img src="Resources/img14.jpg" alt="" title=""/></li>
<li><img src="Resources/img15.jpg" alt="" title=""/></li>
<li><img src="Resources/img17.jpg" alt="" title=""/></li>
<li><img src="Resources/img2.jpg" alt="" title=""/></li>
<li><img src="Resources/img5.jpg" alt="" title=""/></li>
<li><img src="Resources/img3.jpg" alt="" title=""/></li>
</ul>
Table Structure
表结构
User Name nvarchar(50)
Pic Path nvarchar(MAX)
采纳答案by Tim B James
For what you are trying to achieve, it would be best and easiest just to use a <asp:ListView>control.
对于您想要实现的目标,最好和最简单的方法是使用<asp:ListView>控件。
There is a good tutorial here on how to use it, and pretty much similar to what you are doing http://weblogs.asp.net/scottgu/archive/2007/08/10/the-asp-listview-control-part-1-building-a-product-listing-page-with-clean-css-ui.aspx
这里有一个关于如何使用它的很好的教程,与您正在做的非常相似http://weblogs.asp.net/scottgu/archive/2007/08/10/the-asp-listview-control-part -1-building-a-product-listing-page-with-clean-css-ui.aspx
It would basically involve you creating a <asp:ListView>control like;
它基本上会涉及您创建一个<asp:ListView>控件,例如;
<asp:ListView ID="ListView1" runat="server">
<LayoutTemplate>
<ul>
<asp:PlaceHolder ID="itemPlaceholder" runat="server" />
</ul>
</LayoutTemplate>
<ItemTemplate>
<li>
<img src='<%#Eval("PicPath")%>' alt='<%#Eval("UserName")%>' />
</li>
</ItemTemplate>
<EmptyDataTemplate>
<p>Nothing here.</p>
</EmptyDataTemplate>
</asp:ListView>
Then binding your data to it.
然后将您的数据绑定到它。
this.ListView1.DataSource = YourDataSource;
this.ListView1.DataBind();
回答by Imran Rizvi
I suppose your datasource is a DataSet ds that has one DataTable and a field picpath, then you can write the iteration directly in aspx
我想你的数据源是一个 DataSet ds,它有一个 DataTable 和一个字段 picpath,然后你可以直接在 aspx 中编写迭代
<ul id="">
<% foreach (DataRow dr in ds.Tables[0].Rows) { %>
<li><img src="<% dr[\"picpath\"].ToString() %>" alt="" title=""/></li>
<% } %>
</ul>
To do it server side see the accepted answer in below link see the accepted answer in the following link Rendering an unordered list using asp.net
要做到这一点,服务器端请参阅以下链接中接受的答案 请参阅以下链接中接受的答案使用 asp.net 呈现无序列表

