Search code examples
c#asp.nethtmlcontrolslistviewitem

Change order of listview based on Control inside listviewitem


I'm trying to create a blog page using a ListView. I am creating a reply function which adds a other css style to the listview when the message contains a parentmessageid.

This is working. Now I need to change the order of the message so the reply message will be placed under the parentmessage. Below you will find my implementation:

Function for datasource to fill the Listview:

protected void LoadMessages(int userid)
{
    Bericht berichten = new Bericht();
    if (berichten.LaadBerichten(userid).Tables.Count > 0)
    {
        ListViewMessages.DataSource = berichten.LaadBerichten(userid);
        ListViewMessages.DataBind();
    }
}

Function to add css style on items where LabelMessageID contains a value:

protected void ListItemMessages_Load(object sender, EventArgs e)
{
    HtmlGenericControl li = (HtmlGenericControl)sender;
    ListViewItem container = (ListViewItem)li.NamingContainer;
    Label LabelParentMessageID = (Label)container.FindControl("LabelParentMessageID");

    if (LabelParentMessageID.Text != string.Empty)
    {
        li.Attributes.Add("class", "reply");
    }
}

ASP.NET ListView Source:

<asp:ListView ID="ListViewMessages" runat="server">
    <ItemTemplate>
        <li id="ListItemMessages" runat="server" onload="ListItemMessages_Load">
            <img src="<%# Eval("[imagelocation]")%>" alt="image" />
            <div class="top-pointer"></div>
            <div class="pointer"></div>
            <!--Hidden Controls-->
            <asp:Label ID="LabelMessageID" runat="server" Text='<%# Eval("[messageid]")%>' Visible="false"></asp:Label>
            <asp:Label ID="LabelParentMessageID" runat="server" Text='<%# Eval("[parentmessageid]")%>' Visible="false"/>
        </li>
    </ItemTemplate>
</asp:ListView>

Can someone help me out with changing the order of the items? Because I have no idea how to accomplish this.


Solution

  • Solved using a SQL stored procedure.