C# ASP.NET 中的自定义项目符号列表项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1097916/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 08:09:31  来源:igfitidea点击:

Customized bulleted list items in ASP.NET

c#asp.nethtml

提问by sudarsanyes

I am just a beginner in ASP.NET. My question is simple, I wanna add list items dynamically from the code behind file and I want each item to have a text and couple of images as hyperlinks. The HTML sample should be like,

我只是 ASP.NET 的初学者。我的问题很简单,我想从文件背后的代码动态添加列表项,我希望每个项目都有一个文本和几个图像作为超链接。HTML 示例应该是这样的,

<ul>
  <li>do foo &nbsp;<a href="#"><img src="some_image.png" /></a></li>
  <li>do bar &nbsp;<a href="#"><img src="some_image.png" /></a></li>
  ...
</ul>

The number of items is dependent on the collection retrieved by the code behind file.

项目数取决于代码隐藏文件检索到的集合。

P.S. my code behind file is written in C#

PS我的文件背后的代码是用C#编写的

采纳答案by Jeff Sternal

The Repeatercontrol is the simplest way to create a customized bulleted list, plus it gives you complete control over the HTML you generate. To use it, set up a template like this:

Repeater控件是创建自定义项目符号列表的最简单方法,而且它使您可以完全控制生成的 HTML。要使用它,请设置如下模板:

<ul>
<asp:Repeater runat="server" ID="ListRepeater">
   <ItemTemplate>
       <li>do foo &nbsp;<a href='#'><img src='<%# Eval("ImageSource") %>' /></a></li>
   </ItemTemplate>
</asp:Repeater>
</ul>

Then in your code-behind (or declaratively in your markup, depending on your preference), set the repeater's data source and bind it:

然后在你的代码隐藏中(或在你的标记中声明,取决于你的偏好),设置转发器的数据源并绑定它:

void Page_Load(object sender, EventArgs e) {
  // Some method you've defined to get your images
  List<string> imageList  = GetImages();
  ListRepeater.DataSource = imageList;
  ListRepeater.DataBind();
}

ASP.NET renders the template once for each item in your data source.

ASP.NET 为数据源中的每个项目呈现一次模板。

The Repeatercontrol has more features than what I've shown here, but this should get you started. Good luck!

Repeater控件的功能比我在这里展示的要多,但这应该能让您入门。祝你好运!



Edit: a year after writing this answer, I still think repeaters are the best option among server controls, but more and more I prefer foreachstatements right in my .aspx templates:

编辑:写完这个答案一年后,我仍然认为中继器是服务器控件中的最佳选择,但我越来越喜欢foreach.aspx 模板中的语句:

<ul>
    <% foreach(Image image in this.Images) { %>
        <li>do foo &nbsp;<a href='#'><img src='<%= image.Source %>' /></a></li>
    <% } %>
</ul>

回答by leppie

Just use the Repeater control. Simply and easy. :)

只需使用中继器控件。简单易行。:)

回答by William Edmondson

ASP.Net BulletedList. MSDN

ASP.Net 项目符号列表。MSDN