Friday, October 31, 2014

Simple Pagination through query string :-




 1) Add variable to page count like
int itemPageCount = 15;
2) Get the total items
int totalItem = totalSelectedItems.Count;
3) How much pages will be created
int totalPage;
 int totalPageCheck = totalItem % itemsPerPage;
  if (totalPageCheck == 0) totalPage = totalItem / itemsPerPage;
                else totalPage = (totalItem / itemsPerPage) + 1;

4) Set default page number to 1

var pageNumber = 1;

-----------
 if (!string.IsNullOrEmpty(Request.QueryString["page"]))
                {
                    int num;
                    bool res = int.TryParse(Request.QueryString["page"], out num);
                    if (res)
                        pageNumber = num;
                }
                if (isButton)
                {
                    pageNumber = 1;
                }
                if (pageNumber == 1)
                {
                    individualItems = totalSelectedItems.Take(itemsPerPage).ToList();
                }
                else
                {
                    individualItems = totalSelectedItems.Skip(itemsPerPage * (pageNumber - 1)).Take(itemsPerPage).ToList();
                }



5) Write code section to print record from totalSelectedItems

6) Code for generating the querysting

  if (totalItem > itemsPerPage)
                    {
                        SimpleControl textCenterDiv = SimpleControl.RenderSimple(HtmlTextWriterTag.Div,
                                                                                 "text-center", string.Empty);
                        pagination.Controls.Add(textCenterDiv);

                        SimpleControl paginationUl = SimpleControl.RenderSimple(HtmlTextWriterTag.Ul,
                                                                                "pagination", string.Empty);
                        textCenterDiv.Controls.Add(paginationUl);
                        if (pageNumber == 1)
                        {
                            SimpleControl paginationPreviousLi = SimpleControl.RenderSimple(HtmlTextWriterTag.Li,
                                                                                        "disabled",
                                                                                        string.Empty);
                            paginationUl.Controls.Add(paginationPreviousLi);
                            SimpleControl previousTag = SimpleControl.RenderSimpleLink("<", string.Empty,
                                                                                       string.Empty,
                                                                                       string.Empty);
                            paginationPreviousLi.Controls.Add(previousTag);
                        }
                        else
                        {
                            SimpleControl paginationPreviousLi = SimpleControl.RenderSimple(HtmlTextWriterTag.Li,
                                                                                        string.Empty,
                                                                                        string.Empty);
                            paginationUl.Controls.Add(paginationPreviousLi);
                            var uri = HttpContext.Current.Request.Url.AbsoluteUri.Split(new char[] { '?' });
                            SimpleControl previousTagClickable = SimpleControl.RenderSimpleLink("<",
                                                                                                uri[0] +
                                                                                                "?page=" +
                                                                                                (pageNumber - 1) + "&segment=" + ddlItems.SelectedItem.Value.Trim() + "&filter=" + ddlletterFilter.SelectedItem.Value.Trim(),
                                                                                                string.Empty,
                                                                                                string.Empty);
                            paginationPreviousLi.Controls.Add(previousTagClickable);
                        }


                        for (int i = 1; i <= totalPage; i++)
                        {
                            var uri = HttpContext.Current.Request.Url.AbsoluteUri.Split(new char[] { '?' });

                            if (pageNumber == i)
                            {
                                SimpleControl paginationLi = SimpleControl.RenderSimple(HtmlTextWriterTag.Li,
                                                                                    "active", string.Empty);
                                paginationUl.Controls.Add(paginationLi);
                                SimpleControl pageActive = SimpleControl.RenderSimple(HtmlTextWriterTag.A,
                                                                                      string.Empty, string.Empty,
                                                                                      i.ToString());
                                paginationLi.Controls.Add(pageActive);
                            }
                            else
                            {
                                SimpleControl paginationLi = SimpleControl.RenderSimple(HtmlTextWriterTag.Li,
                                                                                    string.Empty, string.Empty);
                                paginationUl.Controls.Add(paginationLi);
                                SimpleControl pageLink = SimpleControl.RenderSimpleLink(i.ToString(),
                                                                                        uri[0] + "?page=" + i + "&segment=" + ddlItems.SelectedItem.Value.Trim() + "&filter=" +
                                                                                        ddlletterFilter.SelectedItem.Value.Trim(),
                                                                                        string.Empty,
                                                                                        string.Empty);
                                paginationLi.Controls.Add(pageLink);
                            }
                        }
                        if (pageNumber == totalPage)
                        {
                            SimpleControl paginationNextLi = SimpleControl.RenderSimple(HtmlTextWriterTag.Li,
                                                                                    "disabled", string.Empty);
                            paginationUl.Controls.Add(paginationNextLi);
                            SimpleControl NextTag = SimpleControl.RenderSimpleLink(">", string.Empty,
                                                                                   string.Empty, string.Empty);
                            paginationNextLi.Controls.Add(NextTag);
                        }

                        else
                        {
                            SimpleControl paginationNextLi = SimpleControl.RenderSimple(HtmlTextWriterTag.Li,
                                                                                    string.Empty, string.Empty);
                            paginationUl.Controls.Add(paginationNextLi);
                            var uri = HttpContext.Current.Request.Url.AbsoluteUri.Split(new char[] { '?' });
                            SimpleControl NextTag = SimpleControl.RenderSimpleLink(">",
                                                                                   uri[0] + "?page=" +
                                                                                   (pageNumber + 1) + "&segment=" + ddlItems.SelectedItem.Value.Trim() + "&filter=" +
                                                                                   ddlletterFilter.SelectedItem.Value.Trim(),
                                                                                   string.Empty, string.Empty);
                            paginationNextLi.Controls.Add(NextTag);
                        }
                    }



No comments:

Post a Comment