Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fill <ul> with database values

how can I fill an unordered list with values from SQL Server 2008 R2 SP1? xD I have this, using a asp:repeater:

<ul style="list-style:none">
    <asp:Repeater ID="deptList" runat="server">
        <ItemTemplate>
            <li>
                <asp:HyperLink runat="server" 
                Text='<%# Eval("Name") %>'
                NavigateUrl='<%# Link.ToDepartment(Eval("DepartmentID").ToString()) %>'
                />
            </li>
        </ItemTemplate>
    </asp:Repeater>
</ul>

and the output HTML:

<ul style="list-style:none">

            <li>
                <a href="http://...">value1</a>
            </li>

            <li>
                <a href="http://...">value2</a>
            </li>

            <li>
                <a href="http://...">value3</a>
            </li>

</ul>

Is there a better way to do this?

like image 765
Adrianne Avatar asked Oct 20 '11 00:10

Adrianne


1 Answers

You can use a for loop to iterate all the values from the data source. A sample:

<ul>
  <% foreach(var item in Collection) { %>
     <li><%=item.Property%></li>
  <% } %>
</ul>

And also you can use other data controls provided by ASP.NET, such as DataGrid.

And, if you insist using Repeater, I suggest you put <ul> in HeaderTemplate, </ul> into FooterTemplate, this prevents empty <ul></ul>.

like image 54
gekowa Avatar answered Oct 13 '22 20:10

gekowa