Customized bulleted list items in ASP.NET

后端 未结 3 697
别跟我提以往
别跟我提以往 2020-12-17 06:30

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

3条回答
  •  旧时难觅i
    2020-12-17 07:31

    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:

    • do foo  

    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 imageList  = GetImages();
      ListRepeater.DataSource = imageList;
      ListRepeater.DataBind();
    }
    

    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!


    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:

      <% foreach(Image image in this.Images) { %>
    • do foo  
    • <% } %>

提交回复
热议问题