按日期排序仅适用于当前页面,在分页转发器控件内

时间:2011-12-27 22:59:42

标签: c# asp.net linq-to-sql

我正在尝试排序,先显示最新的条目。我有一个转发器控件绑定到pagedDataSource,我的排序只适用于页面而不是整个集合本身。

这是我的代码背后:

    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 = "&nbsp;";
            plcPaging.Controls.Add(spacer);
        }

    }

EDIT ::包含页面控制事件处理程序

    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);
    }

我想要的基本上是让排序工作跨所有页面而不是仅仅一页。请指出我正确的方向。感谢

1 个答案:

答案 0 :(得分:1)

在拍摄之前完成订单。

var query = from q in db.question_tables
            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()
             }
             .Take(take)
             .Skip(pageSize);

您也可以在.Take

之前使用linq .OrderBy方法

然后在分页控件上。

void lbl_Click(object sender, EventArgs e)
{
    LinkButton lnk = sender as LinkButton;
    int currentPage = int.Parse(lnk.Text);
    int take = page.PageSize;
    int skip = page.PageSize * (currentPage - 1); 
    FetchData(take, skip);
}