计算PagedDataSource中的项目?

时间:2013-07-05 20:43:21

标签: c# asp.net

我正在尝试使分页项目计数器类似__的 _ 项目,如1-6 pf 21项目和6-12 of 21.有人可以告诉我该怎么做吗?

private void BindResourcesRepater()
        {

            string tag = Request.QueryString["tag"];
            if (String.IsNullOrEmpty(tag))
            {

                //Guid userID = Guid.Parse(Membership.GetUser(HttpContext.Current.User.Identity.Name).ProviderUserKey.ToString());
                DataSet ds = new DataSet();
                int selectedTopicID = Convert.ToInt32(ddlTopics.SelectedValue);
                int selectedSkillID = Convert.ToInt32(ddlSkills.SelectedValue);
                int selectedTypeID = Convert.ToInt32(ddlTypes.SelectedValue);
                string keyword = txtbKeyword.Text.Trim();
                int sortBy = Convert.ToInt32(ddlSortBy.SelectedValue);
                ds = Resource.Search_Resource(selectedTopicID, selectedSkillID, selectedTypeID, keyword, sortBy);




                PagedDataSource objPds = new PagedDataSource();
                objPds.DataSource = ds.Tables[0].DefaultView;

                objPds.AllowPaging = true;

                objPds.PageSize = 6;


                int curpage;

                if (ViewState["Page"] != null)
                {
                    curpage = Convert.ToInt32(ViewState["Page"]);
                }
                else
                {
                    ViewState["Page"] = 1;
                    curpage = 1;
                }

                // Set the currentindex
                objPds.CurrentPageIndex = curpage - 1;

                // Display the current page
                // lblCurrentPage.Text = "Page: " + (curpage).ToString() + " of " + objPds.PageCount.ToString();

                rp_resList.DataSource = objPds;
                rp_resList.DataBind();

                if (rp_resList.Items.Count == 0)
                {
                    lnkNext.Visible = false;
                    if (Convert.ToInt32(ViewState["Page"]) == 1)
                    {
                        lnkPrev.Visible = false;
                    }
                    else
                    {
                        lnkNext.Visible = false;
                    }
                }


                else
                {
                    lnkNext.Visible = true;
                    if (rp_resList.Items.Count < objPds.PageSize)
                    {
                        lnkNext.Visible = false;
                    }

                    if (Convert.ToInt32(ViewState["Page"]) == 1)
                    {
                        lnkPrev.Visible = false;

                    }
                    else
                    {
                        lnkPrev.Visible = true;
                    }
                }


            }
            else
            {
                DataSet ds = new DataSet();
                int selectedTopicID = Convert.ToInt32(ddlTopics.SelectedValue);
                int selectedSkillID = Convert.ToInt32(ddlSkills.SelectedValue);
                int selectedTypeID = Convert.ToInt32(ddlTypes.SelectedValue);
                txtbKeyword.Text = tag;
                string keyword = tag.ToString();
                int sortBy = Convert.ToInt32(ddlSortBy.SelectedValue);
                ds = Resource.Search_Resource(selectedTopicID, selectedSkillID, selectedTypeID, tag, sortBy);
                lbl_totalResult.Text = ds.Tables[0].Rows.Count.ToString() + " " + "Resources Found";
                rp_resList.DataSource = ds.Tables[0];
                rp_resList.DataBind();
            }
    }

1 个答案:

答案 0 :(得分:0)

试试这个:

private void BindResourcesRepater()
{
    // your existing code...

    int numberOfItems = ds.Tables[0].Rows.Count;
    lblCurrentVisibleItems.Text = GetCurrentVisibleItemsText(numberOfItems, objPds.PageSize, objPds.CurrentPageIndex);
}

private string GetCurrentVisibleItemsText(int numberOfItems, int pageSize, int currentPageIndex)
{
    int startVisibleItems = currentPageIndex * pageSize + 1;
    int endVisibleItems = Math.Min((currentPageIndex + 1) * pageSize, numberOfItems);

    return string.Format("Displaying {0}-{1} of {2} items", startVisibleItems, endVisibleItems, numberOfItems);
}

我不确定要在哪个标签中显示这个标签,但在我的示例中,我使用了一个名为lblCurrentVisibleItems的标签。将其替换为您要使用的任何标签。