排序列表项(最新一期先) [英] Sorting List Items (Latest one first)

查看:196
本文介绍了排序列表项(最新一期先)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有麻烦displayin列表itmes的值按正确的顺序。以下是该列表中的字段(其实际系统名称):

I am having trouble displayin the values of list itmes in the proper order. The following are the fields in the list (with their actual system names):

-Title
- 类别
-NominatedWon
- 标志

-Title -Category -NominatedWon -Logo

标题字段包含年份值如2011,2010等。

The "Title" field contains year values eg, "2011", "2010", etc.

//getting the awards list and extracting correct values from the necesary fields
//asp running with elevated privilegs
SPSecurity.RunWithElevatedPrivileges(delegate()
{

using (SPSite site = new SPSite(webUrl))
{
using (SPWeb web = site.OpenWeb())
{



try
{

SPList awardsList = web.Lists["Awards"];

SPListItemCollection listItemCollection = awardsList.Items;



 //Creating the table
 Table tbl = new Table();

 //foreach (SPListItem oListItem in listItemCollection)
 int x = listItemCollectionI.Count;

 for(int i = 0; (i * 2) < x; i++) // divide total item collection by two, each loop 
 iteration, add two awards to a row (left cell, right cell)
 {
 // get listItemCollection[i];
 //Create table rows, table cells

 int leftIndexer = i * 2;
 int rightIndexer = (i * 2) + 1;

 if (leftIndexer == x)
 {
 break;
 }                                

 TableRow tblRow = new TableRow();
 TableCell tblCellLeft = new TableCell(); //for the awards in the first column
 TableCell tblCellRight = new TableCell(); //for the awards in the second column

 tblCellLeft.VerticalAlign = VerticalAlign.Top;
 tblCellLeft.HorizontalAlign = HorizontalAlign.Center;
 tblCellLeft.CssClass = ("style5");

 tblCellRight.VerticalAlign = VerticalAlign.Top;
 tblCellRight.HorizontalAlign = HorizontalAlign.Center;


 // get the values
 awardYear = listItemCollection[leftIndexer]["Title"].ToString();
 awardCategory = listItemCollection[leftIndexer]["Category"].ToString();
 awardNomWon = listItemCollection[leftIndexer]["NominatedWon"].ToString();

 if(listItemCollection[leftIndexer]["Logo"] != null)
 awardLogo = (string)listItemCollection[leftIndexer]["Logo"];

 // add to left cell
 //values for the left column
 tblCellLeft.Controls.Add(new LiteralControl("<div class=\"style1\">" + awardYear + 
 "</div>"));
 tblCellLeft.Controls.Add(new LiteralControl("<div class=\"style2\">" + awardCategory 
 + "</div>"));
 tblCellLeft.Controls.Add(new LiteralControl("<div class=\"style3\">" + awardNomWon + 
 "</div>"));
 tblCellLeft.Controls.Add(new LiteralControl("<div class=\"style4\">" + "<img src=" + 
 awardLogo.Replace(",", "") + "</div>"));

 // add left cell to row
 tblRow.Cells.Add(tblCellLeft);

 if (rightIndexer < x) // if this item exists in the collection (prevent bug with odd 
 number of awards)
 {


 // get the values
 awardYear = listItemCollection[rightIndexer]["Title"].ToString();
 awardCategory = listItemCollection[rightIndexer]["Category"].ToString();
 awardNomWon = listItemCollection[rightIndexer]["NominatedWon"].ToString();

 if (listItemCollection[rightIndexer]["Logo"] != null)
 awardLogo = (string)listItemCollection[rightIndexer]["Logo"];


 // add to right cell
 //Values for the right column
 tblCellRight.Controls.Add(new LiteralControl("<div class=\"style1\">" + awardYear + 
 "</div>"));
 tblCellRight.Controls.Add(new LiteralControl("<div class=\"style2\">" + awardCategory 
 + "</div>"));
 tblCellRight.Controls.Add(new LiteralControl("<div class=\"style3\">" + awardNomWon + 
 "</div>"));
 tblCellRight.Controls.Add(new LiteralControl("<div class=\"style4\">" + "<img src=" + 
 awardLogo.Replace(",", "") + "</div>"));

 // add right cell to row
 tblRow.Cells.Add(tblCellRight);
 }

 // add row to table
 tbl.Rows.Add(tblRow);

 }

 PlaceHolder6.Controls.Add(tbl); // add table outside of loop 
 }
 catch (Exception err)
 {
 PlaceHolder3.Controls.Add(new LiteralControl(err.ToString()));
 }

 }
 }



 });



所以输出由于某种原因,似乎表明这两个最新的列表中的项目如列表项2012,和列表项2011在底部显示。从理论上讲,这些应该在顶部后跟列表项目的其余部分显示出来。

So the output for some reason seems to show the two latest lists items eg list item "2012", and list item "2011" are displayed at the bottom. In theory these should be displayed at the top followed by the rest of the list items.

在此的任何sugestions将不胜感激!

Any sugestions on this will be greatly appreciated!

感谢

推荐答案

有没有在你的代码来表示的顺序,使用的东西线沿线的:

There's nothing in your code to indicate the order, use something along the lines of:

SPList awardsList = web.Lists["Awards"];                     

SPQuery q = new SPQuery();
q.Query="<OrderBy><FieldRef Name='Title' /></OrderBy>";

SPListItemCollection listItemCollection = awardsList.Items.GetItems(q);   

这篇关于排序列表项(最新一期先)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆