C#手动数字分页


private void aspage(int inPage, int size, string strPage) { // int pageNum = (pageCount % size) == 0 ? pageCount / size : pageCount / size + 1;//页码数 StringBuilder sb = new StringBuilder(); sb.Append("<div class=\"pnav\"><div class=\"pr\">"); //需要判断是否启用--向上页 sb.AppendFormat(inPage == 1 ? "<a class=\"first\" href=\"#\">上一页</a>" : "<a class=\"first\" href=\"" + strPage + "\">上一页</a>", inPage - 1); //大于10条数据时 if (pageNum > 10) { if (inPage > 0 && inPage < 7) { for (int i = 1; i < 10; i++) { sb.AppendFormat(i == inPage ? "<a href=\"#\" class=\"curr\">{0}</a>" : "<a href=\"" + strPage + "\">{0}</a>", i); } sb.Append("<span class=\"duo\">...</span>"); sb.AppendFormat("<a href=\"" + strPage + "\">{0}</a>", pageNum); } else if (inPage > 6 && inPage < (pageNum - 5)) { sb.AppendFormat("<a href=\"" + strPage + "\">1</a>", 1); sb.Append("<span class=\"duo\">...</span>"); for (int i = (inPage - 4); i <= inPage + 4; i++) { sb.AppendFormat(i != inPage ? "<a href=\"" + strPage + "\">{0}</a>" : "<a href=\"" + strPage + "\" class=\"curr\">{0}</a>", i); } sb.Append("<span class=\"duo\">...</span>"); sb.AppendFormat("<a href=\"" + strPage + "\">{0}</a>", pageNum); } else { sb.AppendFormat("<a href=\"" + strPage + "\">1</a>", 1); sb.Append("<span class=\"duo\">...</span>"); for (int i = (pageNum - 8); i <= pageNum; i++) { sb.AppendFormat(i == inPage ? "<a href=\"" + strPage + "\" class=\"curr\">{0}</a>" : "<a href=\"" + strPage + "\">{0}</a>", i); } } } else//小于10条数据 { for (int i = 1; i <= pageNum; i++) { sb.AppendFormat(i == inPage ? "<a href=\"" + strPage + "\" class=\"curr\">{0}</a>" : "<a href=\"" + strPage + "\">{0}</a>", i); } } //需要判断是否启用 sb.AppendFormat(inPage != pageNum ? "<a class=\"first\" href=\"" + strPage + "\">下一页</a>" : "<a class=\"first\" href=\"#\">下一页</a>", inPage + 1); sb.Append("</div></div>"); Label1.Text = sb.ToString(); }
html, body, div, h1, h2, h3, dd, dl, dt, li, ol, ul{margin:0;padding:0;}
body{ font: 12px arial,宋体b8b\4f53; background-repeat: repeat; background-color: #E7E7E7; background-image: url(img/bj.jpg); background-position: 0px 0px; }
div {text-align:left;overflow:hidden; zoom:1; }
h1, h2, h3, h4, h5, h6{font-size:100%;}
li{list-style-type:none;}
a{text-decoration:none;color:#414141;}
img {border:0;}
input{ font-family: inherit; font-size: inherit; font-weight: inherit; resize: none; outline: 0; }
        /*内容部分 end*/        
.pnav{ margin-right: auto; margin-left: auto; border-bottom: 1px solid #E6E6E6; box-shadow: 0 1px 0 rgba(255,255,255,.6);  788px; margin-top: 40px; margin-bottom: 30px; }
.pnav .pr{ float: right; }
.pnav a { background: #ffffff; color: #666; float: left; height: 28px; line-height: 28px; border-top- 1px; border-bottom- 1px; border-left- 1px; border-top-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: #D6D5D3; border-bottom-color: #D6D5D3; border-left-color: #D6D5D3; font-weight: bold; font-size: 14px;  50px; text-align: center; }
.pnav a:hover{ background-color: #E3E3E3; }
.pnav a.first { border-radius: 2px 0px 0px 2px;  70px; color: #666; background-color: #E3E3E3; }
.pnav a.last { border-radius: 0px 2px 2px 0px;  70px; color: #666; border-right- 1px; border-right-style: solid; border-right-color: #D6D5D3; background-color: #E3E3E3; }
.pnav a.curr{ background-color: #E3E3E3; color: #C71422; }
.pnav .duo{ background: #ffffff; color: #666; float: left; height: 28px; line-height: 22px; border-top- 1px; border-bottom- 1px; border-left- 1px; border-top-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: #E3E2E1; border-bottom-color: #E3E2E1; border-left-color: #E3E2E1; font-weight: bold;  40px; text-align: center; }

//固定个数有个小BUG,但不影响便用

private void aspage(int inPage, int size, string strPage)
        {
            //只有12页时有个小BUG
            int pageNum = (pageCount % size) == 0 ? pageCount / size : pageCount / size + 1;//页码数
            StringBuilder sb = new StringBuilder();
            sb.Append("<div class=\"pnav\"><div class=\"pr\">");
            //需要判断是否启用--向上页
            sb.AppendFormat(inPage == 1 ? "<a class=\"first\" href=\"#\">上一页</a>" : "<a class=\"first\" href=\"" + strPage + "\">上一页</a>", inPage - 1);
            //大于12条数据时
            if (pageNum > 11)
            {
                if (inPage > 0 && inPage < 7)
                {
                    for (int i = 1; i < 12; i++)
                    {
                        sb.AppendFormat(i == inPage ? "<a href=\"#\" class=\"curr\">{0}</a>" : "<a href=\"" + strPage + "\">{0}</a>", i);
                    }
                    sb.Append("<span class=\"duo\">...</span>");
                    sb.AppendFormat("<a href=\"" + strPage + "\">{0}</a>", pageNum);
                }
                else if (inPage > 6 && inPage < (pageNum - 5))
                {
                    sb.AppendFormat("<a href=\"" + strPage + "\">1</a>", 1);
                    sb.Append("<span class=\"duo\">...</span>");
                    for (int i = (inPage - 4); i <= inPage + 4; i++)
                    {
                        sb.AppendFormat(i != inPage ? "<a href=\"" + strPage + "\">{0}</a>" : "<a href=\"" + strPage + "\" class=\"curr\">{0}</a>", i);
                    }
                    sb.Append("<span class=\"duo\">...</span>");
                    sb.AppendFormat("<a href=\"" + strPage + "\">{0}</a>", pageNum);
                }
                else
                {
                    sb.AppendFormat("<a href=\"" + strPage + "\">1</a>", 1);
                    sb.Append("<span class=\"duo\">...</span>");
                    for (int i = (pageNum - 10); i <= pageNum; i++)
                    {
                        sb.AppendFormat(i == inPage ? "<a href=\"" + strPage + "\" class=\"curr\">{0}</a>" : "<a href=\"" + strPage + "\">{0}</a>", i);
                    }
                }
            }
            else//小于11条数据
            {
                for (int i = 1; i <= pageNum; i++)
                {
                    sb.AppendFormat(i == inPage ? "<a href=\"" + strPage + "\" class=\"curr\">{0}</a>" : "<a href=\"" + strPage + "\">{0}</a>", i);
                }
            }
            //需要判断是否启用
            sb.AppendFormat(inPage != pageNum ? "<a class=\"last\" href=\"" + strPage + "\">下一页</a>" : "<a class=\"last\" href=\"#\">下一页</a>", inPage + 1);
            sb.Append("</div></div>");
            Label1.Text = sb.ToString();
        }

  

原文地址:https://www.cnblogs.com/chixiaojin/p/3138031.html