Paging in ASP.NET without a database

I have a page on my ASP.NET site that has a Repeater element to display messages from site members.

At the moment, I save the data for the messages in an XML file, and then cache them inside the site inside user objects.

So, I have a:

public class MemberPost
{
    public string Title { get; set; }
    public string Text { get; set; }
    public string Name { get; set; }
    public DateTime Date { get; set; }
    public List<string> Pictures { get; set; }
}

and

public class MemberPosts : List<MemberPost>
{

}

I can set the repeater data source to an instance of MemberPosts, and everything works as expected, but I want to add paging to add additional messages.

All the examples that I find seem to be related to the fact that the data should be uploaded to the database - is there a way to bind a relay or other similar control to my MemberPosts collection in memory and does it swap for me?

VS2010/.NET 3.5, 4.0, .

.

+3
3

" ", , , : -)

protected void Page_Load(object sender, EventArgs e)
{
    IEnumerable<MemberPost> posts = from post in MemberPostCacher.Instance.Posts
                                    orderby post.Date descending
                                    select post;

    memberPostsRepeater.DataSource = GetPageMemberPosts(posts);
    memberPostsRepeater.DataBind();
}

private IEnumerable<MemberPost> GetPageMemberPosts(IEnumerable<MemberPost> posts)
{
    int totalNumberOfPosts = posts.Count();
    int pageSize = int.Parse(ConfigurationManager.AppSettings["MemberPostsPageSize"]);
    int totalPages = GetTotalPages(totalNumberOfPosts, pageSize);
    int currentPage = 1;

    if (Request.QueryString["page"] != null)
    {
        int.TryParse(Request.QueryString["page"], out currentPage);
        if (currentPage < 1 || currentPage > totalPages)
        {
            currentPage = 1;
        }
    }

    SetIntroVisibility(currentPage);
    SetLinks(currentPage, totalPages);
    SetPageNumberText(currentPage, totalPages);

    return posts.Skip((currentPage - 1) * pageSize).Take(pageSize);
}

private int GetTotalPages(int numberOfPosts, int pageSize)
{
    return numberOfPosts % pageSize == 0
                ? numberOfPosts / pageSize
                : numberOfPosts / pageSize + 1;
}

private void SetIntroVisibility(int currentPage)
{
    membersIntro.Visible = currentPage == 1;
}

private void SetLinks(int currentPage, int totalPages)
{
    linkPrevious.Visible = currentPage != 1;
    linkNext.Visible = currentPage != totalPages;

    linkPrevious.NavigateUrl = FormatPageLink(currentPage - 1);
    linkNext.NavigateUrl = FormatPageLink(currentPage + 1);
}

private string FormatPageLink(int pageNumber)
{
    return string.Format("{0}{1}{2}",
                            Request.CurrentExecutionFilePath,
                            "?page=",
                            pageNumber);
}

private void SetPageNumberText(int currentPage, int totalPages)
{
    lblNumRecords.Text = string.Format("&nbsp;&nbsp;Page {0} of {1}&nbsp;&nbsp;", currentPage, totalPages);
    if(totalPages == 1)
    {
        lblNumRecords.Visible = false;
    }
}
+3

objectdatasource ! .........

0

Source: https://habr.com/ru/post/1759755/


All Articles