ASP.NET中的自定义项目符号列表项 [英] Customized bulleted list items in ASP.NET

查看:129
本文介绍了ASP.NET中的自定义项目符号列表项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是在ASP.NET初学者。我的问题很简单,我想从后面文件中的code动态地添加列表中的项目,我想每个项目有超链接图片的文字和情侣。在HTML样本应该是这样的,

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,

<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>

的项目数是依赖于由后面文件code检索集合

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

P.S。隐藏文件我的code是用C#

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

推荐答案

的<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.repeater.aspx\"><$c$c>Repeater控制是创建一个自定义的项目符号列表最简单的方法,再加上它可以让你在生成HTML完全控制。要使用它,设立这样的一个模板:

The Repeater control 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:

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

然后在你的code-落后(在您的标记或声明,这取决于你的preference),设置直放站的数据源,并将其绑定:

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渲染模板一次为您的数据源中的每个项目。

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

转发控制有比这里已经证明了我更多的功能,但这应该让你开始。祝你好运!

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

修改:一年写这个答案后,我仍然认为中继器服务器控件中最好的选择,但我越来越preFER 的foreach 在我的.aspx模板正确语句:

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

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

这篇关于ASP.NET中的自定义项目符号列表项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆