custom pagination in C#





0
Date Submitted Thu. Mar. 19th, 2009 7:00 AM
Revision 1 of 1
Scripter mycodeofshailendra
Tags C | custom | in | Pagination | Using
Comments 0 comments
Method to show custom Paginatio

//url : page ulr
//max_links : how many link u want to show on page (e.g. 10)
//curr_pages : current Navigation Page;
//NUM_ROWS: to show record per page (e.g 10 )
// back_forward: to show back and forward link
//  css_current: To Apply CSS
...
public string Navigation(string url, int max_links, int curr_pages, int NUM_ROWS, int total_rows, bool back_forward, string css_current)
        {
            string separator = "|";
            int all_pages = (total_rows / NUM_ROWS) - 1;
            string navi_string = "";
            int start = 0;
            int end = 0;
            int forward = 0;
            int backward = 0;
            if (back_forward == false)
            {
                max_links = (max_links < 2) ? 2 : max_links;

            }
            if (curr_pages <= all_pages && curr_pages >= 0)
            {
                if (curr_pages > (max_links / 2))
                {
                    start = (curr_pages - (max_links / 2) > 0) ? curr_pages - (max_links / 2) : 1;
                    end = curr_pages + (max_links / 2);
                    if (end >= all_pages)
                    {
                        end = all_pages + 1;
                        start = (all_pages - (max_links - 1) > 0) ? all_pages - (max_links - 1) : 1;
                    }
                }
                else
                {
                    start = 0;
                    end = (all_pages >= max_links) ? max_links : all_pages + 1;
                }
                if (all_pages >= 1)
                {
                    forward = curr_pages + 1;
                    backward = curr_pages - 1;
                    if (curr_pages > 0)
                    {
                        navi_string = "<a href=\"" + url + "page=" + backward + "\">&lt;&lt;</a>&nbsp;";
                    }
                    else
                    {
                        navi_string = "&lt;&lt;&nbsp;";
                    }
                    if (!back_forward)
                    {
                        for (int a = start + 1; a <= end; a++)
                        {
                            int theNext = a - 1;
                            if (theNext != curr_pages)
                            {
                                navi_string += "<a href=\"" + url + "page=" + theNext + "\">";
                                navi_string += a.ToString() + "</a>";
                                navi_string += (theNext < (end - 1)) ? separator : "";

                            }
                            else
                            {
                                navi_string += (css_current != "") ? "<span class=\"" + css_current + "\">" + a.ToString() + "</span>" : a.ToString();
                                navi_string += (theNext < (end - 1)) ? separator : "";
                            }

                        }
                    }
                    navi_string += (curr_pages < all_pages) ? "&nbsp;<a href=\"" + url + "page=" + forward.ToString() + "\">&gt;&gt;</a>" : "&nbsp;&gt;&gt;";
                }

            }
            return navi_string;
        }
usage;
 int MaxLinks = 10;
    int CurrentPage = 0;
    int RecordPerPage = 10;
if (Request.Params["page"] != null)
        {
            CurrentPage = Int32.Parse(Request.Params["page"]);
        }
Navigation("default.aspx?", MaxLinks, CurrentPage, RecordPerPage, Ds.Tables[0].Rows.Count, false, "");
 

shailendra bhadange

shailendra-problemsolution.blogspot.com/
Shailendra Bhadange

Comments

There are currently no comments for this snippet.

Voting

Votes Up


Votes Down