I am trying to do a sort here to display the latest entries first. I have a repeater control which is bound to a pagedDataSource, and my sorting only works for page wise rather than the whole collection itself.
here is my code behind :
private int RowCount
{
get
{
return (int)ViewState["RowCount"];
}
set
{
ViewState["RowCount"] = value;
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
FetchData(5, 0);
}
else
{
plcPaging.Controls.Clear();
CreatePagingControl();
}
}
private void FetchData(int take, int pageSize)
{
using (krystaladbDataContext db = new krystaladbDataContext())
{
var query = from q in db.question_tables
.Take(take)
.Skip(pageSize)
orderby q.QUEST_POSTED_DATE descending
select new
{
QUEST_ID = q.QUEST_ID,
QUEST_TEXT = q.QUEST_TEXT,
QUEST_POSTED_DATE = q.QUEST_POSTED_DATE,
QUEST_USR_ID = q.QUEST_USR_ID,
QUEST_LIKES_COUNT = q.QUEST_LIKES_COUNT,
QUEST_REPORT_COUNT = q.QUEST_REPORT_COUNT,
Count = db.question_tables.Count()
};
PagedDataSource page = new PagedDataSource();
page.AllowCustomPaging = true;
page.AllowPaging = true;
page.DataSource = query;
page.PageSize = 5;
QRep.DataSource = page;
QRep.DataBind();
if (!IsPostBack)
{
RowCount = query.First().Count;
CreatePagingControl();
}
}
}
private void CreatePagingControl()
{
for (int i = 0; i < (RowCount / 5) + 1; i++)
{
LinkButton lnk = new LinkButton();
lnk.Click += new EventHandler(lbl_Click);
lnk.ID = "lnkPage" + (i + 1).ToString();
lnk.Text = (i + 1).ToString();
plcPaging.Controls.Add(lnk);
Label spacer = new Label();
spacer.Text = " ";
plcPaging.Controls.Add(spacer);
}
}
EDIT:: included the page control event handler
void lbl_Click(object sender, EventArgs e)
{
LinkButton lnk = sender as LinkButton;
int currentPage = int.Parse(lnk.Text);
int take = currentPage * 5;
int skip = currentPage == 1 ? 0 : take - 5;
FetchData(take, skip);
}
What I want is basically to have the sort work across all the pages rather than for just one page. Please point me in the right direction guys. Thanks
Do the order by before the take.
You could alternatively use the linq .OrderBy method before the
.TakeThen on the paging control.