Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I'm working with a repeater control that displays my list items. Everytime i add an listitem it displays it on my repeater. But i want the repeater to display the latest added item at the top.

Any suggestions?

Right now i'm using this code:

    &nbsp;<asp:Repeater ID="RepeaterContent" runat="server">
    <ItemTemplate>
        <h1><%# ((SPListItem)Container.DataItem)["Title"].ToString() %></h1>
        <i><%# ((SPListItem)Container.DataItem)["Date"].ToString()%></i><br/>   
        <i><%# ((SPListItem)Container.DataItem)["Authors"].ToString()%></i><br/>

        <b><%# ((SPListItem)Container.DataItem)["Contents"].ToString().Substring(0, 20)%></b><br/>

        <%--<i>...Read more</i>--%>

        <a href="<%# (new SPFieldUrlValue(((SPListItem)Container.DataItem)["Links"].ToString()).Url) %> " target="_blank"><%# (new SPFieldUrlValue(((SPListItem)Container.DataItem)["Links"].ToString()).Url) %></a>
    </ItemTemplate>
</asp:Repeater>

and

            using (SPSite site = new SPSite("http://knowitintranet:9797/sites/News"))
            {
                SPWeb web = SPContext.Current.Web;
                SPList list = web.Lists.TryGetList(DropDownListCategory2.SelectedItem.Value);

                SPListItemCollection items = list.GetItems();

                RepeaterContent.DataSource = items;
                RepeaterContent.DataBind();


                site.Dispose();
            }
share|improve this question

1 Answer 1

up vote 1 down vote accepted

Kristian,

Instead of

using (SPSite site = new SPSite("http://knowitintranet:9797/sites/News"))
{
     SPWeb web = SPContext.Current.Web;
     SPList list = web.Lists.TryGetList(DropDownListCategory2.SelectedItem.Value);

     SPListItemCollection items = list.GetItems();

     RepeaterContent.DataSource = items;
     RepeaterContent.DataBind();

     site.Dispose();
 }

You can use:

using (SPSite site = new SPSite("http://knowitintranet:9797/sites/News"))
{
     SPWeb web = SPContext.Current.Web;
     SPList list = web.Lists.TryGetList(DropDownListCategory2.SelectedItem.Value);

     SPQuery query = new SPQuery();
     query.Query = "<OrderBy><FieldRef Name='Created' Ascending='FALSE' /></OrderBy>";
     SPListItemCollection items = list.GetItems(query);

     RepeaterContent.DataSource = items;
     RepeaterContent.DataBind();

     //site.Dispose();
 }

In this way the query will return data ordering them with last modified date descending order, also you don't need to Dispose site object... Since its already in using, it will get disposed after the using block!

I hope this helps...

share|improve this answer
    
Thank you Arsalan! It worked perfectly. Your the man :) –  Kristian Salov Jan 23 '13 at 12:36
    
Thank you! :) It's working –  Kristian Salov Jan 23 '13 at 12:36

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.