Click here to Skip to main content
15,901,001 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
>I'm sort of new to web forms but i hope anyone of you would be able to advise me on how to tackle this:

I have the following table in the database:

ID | Name | ParentId
1 | music | 0
2 | house | 1
3 | urban | 1
4 | games | 0
5 | ps4 | 4
2 | Xbox | 4

*code behind the Aspx page:
I'm just returning a list of the table here like this:


C#
protected List<EventsTable> events;
       events = db.EventsTable.ToList();

*Aspx Page:
on this page i need to list all the events in table.
for example; i need to list a parent Event and its child events below . i.e
* music (parent)
house
urban
*games(parent)
ps4
xbox

This is what i have done so far.
As i mentioned i'm new to web forms but i have experience in MVC , so i'm trying to use the Aspx page as i would in a view in MVC.
I have a table like so:

C#
<table class="table">
                             <tr>
                                 <th>Event</th>
                                  <th>Edit</th>
                             </tr>
                      
                       <%foreach(var s in events) {%>
                             <tbody>
                                    <tr> 
                                 <%if (s.ParentId == 0)
                                     {%>
                                        <td> * <%= s.Name %></td>
                                  <%} %>
                              
                                      <td> <%= s.Name %></td>
                                     <td> <a> Edit</a></td>
                                 </tr>
                             </tbody>
                     
                    
                      <%}%>
                        
                          </table>

Could someone please direct me on how I would go about in listing the events in the way I've said above?
I'm not sure what the best option would be for me to achieve this in web forms.

What I have tried:

Quote:
C#
<table class="table">
                             <tr>
                                 <th>Event</th>
                                  <th>Edit</th>
                             </tr>
                      
                       <%foreach(var s in events) {%>
                             <tbody>
                                    <tr> 
                                 <%if (s.ParentId == 0)
                                     {%>
                                        <td> * <%= s.Name %></td>
                                  <%} %>
                              
                                      <td> <%= s.Name %></td>
                                     <td> <a> Edit</a></td>
                                 </tr>
                             </tbody>
                     
                    
                      <%}%>
                        
                          </table>
Posted
Updated 21-Aug-17 4:14am

1 solution

Try something like this:

Code-behind:
C#
public IQueryable<EventsTable> GetTopLevelEvents()
{
    return db.EventsTable.Where(e => e.ParentId == 0);
}

public IQueryable<EventsTable> GetChildEvents(EventsTable parent)
{
    return db.EventsTable.Where(e => e.ParentId == parent.Id);
}

Markup:
<asp:ListView runat="server" SelectMethod="GetTopLevelEvents" ItemType="YourNamespace.EventsTable">
<LayoutTemplate>
    <table class="table">
    <thead>
        <tr>
            <th>Event</th>
            <th>Edit</th>
        </tr>
    </thead>
    <tbody>
        <tr id="itemPlaceholder" runat="server"/>
    </tbody>
    </table>
</LayoutTemplate>
<ItemTemplate>
    <tr>
        <th>* <%#: Item.Name %></th>
        <td>
            <a>Edit</a>
        </td>
    </tr>
    
    <asp:ListView runat="server" DataSource='<%# GetChildEvents(Item) %>' ItemType="YourNamespace.EventsTable">
    <ItemTemplate>
        <tr>
            <th><%#: Item.Name %></th>
            <td>
                <a>Edit</a>
            </td>
        </tr>
    </ItemTemplate>
    </asp:ListView>
</ItemTemplate>
</asp:ListView>

Model Binding and Web Forms in Visual Studio 2013 | Microsoft Docs[^]
ListView Class | Microsoft Docs[^]
 
Share this answer
 
v2
Comments
1Future 21-Aug-17 14:26pm    
I think you may have just saved my life ! thank you so much

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900