How to add an 'ul' in an 'li' HTML element via C # code?

3

I'm going to have a list with multiple clients, and I wanted to add those clients to a ul element of the HTML element, you have to add those clients dynamically (since each search performed on the system can bring a number of clients) via C # code?

    
asked by anonymous 16.09.2015 / 22:37

2 answers

5

You can do this (within your .aspx file):

<ul>
    <% var numeros = new List<int>{1, 2, 3};
       foreach (int i in lista)
        {
            %> <li> <%: i %> </li>
     <% } %>
</ul>

In this example, I'm creating a list with three numbers and printing each one of them in the <li> tag.

The only thing you need to adapt is to put your list in foreach .

    
16.09.2015 / 22:54
1

Here's another example that can be done by .aspx.cs (CodeBehind)

using System.IO;   

public partial class WebForm1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        List<Cliente> lstCliente = new List<Cliente>
        {
            new Cliente{ Id = 1, Nome = "Cliente1"},
            new Cliente{ Id = 2, Nome = "Cliente2"},
            new Cliente{ Id = 3, Nome = "Cliente3"},
            new Cliente{ Id = 4, Nome = "Cliente4"},
        };

        StringWriter stringWriter = new StringWriter();
        HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);

        htmlWriter.RenderBeginTag(HtmlTextWriterTag.Ul); //Cria a tag ul

        foreach (Cliente cliente in lstCliente)
        {
            htmlWriter.RenderBeginTag(HtmlTextWriterTag.Li); //Cria tag li
            htmlWriter.Write(string.Concat(cliente.Id, ": ", cliente.Nome));
            htmlWriter.RenderEndTag(); //Fecha tag li
        }                       

        htmlWriter.RenderEndTag(); Fecha tag ul

        ltListaClientes.Text = stringWriter.ToString();
    }
}

.aspx I added a literal to receive the list

<asp:Literal id="ltListaClientes" runat="server" />
    
12.11.2015 / 16:47