关于作者

asp的编程思想做了一个.net的分页导航条

上一篇 / 下一篇  2007-12-13 16:24:37 / 个人分类:asp.net

查看( 103 ) / 评论( 2 )
asp原文

css样式
.p_btns { margin: 10px 0 30px 0; }
.p_bar em { line-height: 26px; }
.p_bar a {color: #6774A8; text-decoration:none;}
.p_bar a:hover {text-decoration:underline;}
.p_bar a:link {text-decoration:none;}
.p_bar a:active {text-decoration:underline;}
.p_bar a:visited {text-decoration:none;}
.p_bar {float: left; border: 1px solid #CAD9EA; background: #F7F7F7; height: 24px; line-height: 26px; color: #999; overflow: hidden; }
.p_bar a, .p_bar strong, .p_bar em, .p_bar kbd, #multipage .p_bar em { float: left; padding: 0 8px; line-height:26px; }
.p_bar a:hover { background-color: #FFF; }
.p_bar strong { font-weight: bold; color: #090; background: #CAD9EA; }
.p_bar a.prev, .p_bar a.next { line-height: 24px; font-family: Verdana, Arial, Helvetica, sans-serif; }   
.p_bar a.next { padding: 0 15px; }
.p_bar kbd { border-left: 1px solid #CAD9EA; margin: 0; }
* html .p_bar kbd { padding: 1px 8px; }
.p_bar kbd input { border: 1px solid #CAD9EA; margin-top: 3px !important; * > margin-top: 1px  !important; margin: 1px 4px 0 3px; padding: 0 2px; height: 17px; }
.p_bar kbd>input { margin-bottom: 2px; }

CODE:

<%
Dim page, countNum, pageSize
page = Int(Request.QueryString("page"))
countNum = 123456 ''记录总数
pageSize = 50 ''每页记录数
Response.Write(AutoGoPList(countNum, pageSize, page))

        ''' <summary>
        ''' 当前地址前缀
        ''' </summary>

        Function GetUrlPrefix()
                Dim strUrl, result_url
                strUrl = Request.ServerVariables("Url")
                If Trim(Request.QueryString) <> "" Then ''如果有参数
                        If (Left(Request.ServerVariables("Query_String"), 5) = "page=") Then ''只有页参数
                                result_url = strUrl + "?page="
                        Else
                                Dim strUrl_left
                                strUrl_left = Split(Request.ServerVariables("Query_String"), "page=")
                                If (UBound(strUrl_left) = 0) Then ''没有页参数
                                        result_url = strUrl & "?" + strUrl_left(0) & "&page="
                                Else
                                        result_url = strUrl & "?" + strUrl_left(0) & "page="
                                End If
                        End If
                Else
                        result_url = strUrl & "?page="
                End If
                GetUrlPrefix = result_url
        End Function

        ''' <summary>
        ''' 职能分页
        ''' </summary>
        ''' <param name="countNum">记录数</param>
        ''' <param name="currentPage">第几页</param>
        ''' <returns></returns>

        Function AutoGoPList(countNum, PSize, currentPage)
                Dim UrlPrefix
                UrlPrefix = GetUrlPrefix()
                Dim pageStr
                pageStr = "<div class=""p_btns"">"
                If (countNum > PSize) Then
                        Dim pageCount
                        If countNum Mod PSize = 0 Then
                                pageCount = countNum \ PSize
                        Else
                                pageCount = countNum \ PSize + 1
                        End If
                        If currentPage > pageCount Then
                                currentPage = pageCount
                        End If
                        If currentPage = 0 Then
                                currentPage = 1
                        End If
                        Dim stepNum
                        stepNum = 3
                        Dim pageRoot
                        pageRoot = 1
                        If pageCount = 0 Then
                                pageCount = 1
                        End If

                        pageStr = pageStr + "<div class=""p_bar""><em> " & countNum & " </em>"
                        If (currentPage - stepNum < 2) Then
                                pageRoot = 1
                        Else
                                pageRoot = currentPage - stepNum
                        End If
                        Dim pageFoot
                        pageFoot = pageCount
                        If (currentPage + stepNum >= pageCount) Then
                                pageFoot = pageCount
                        Else
                                pageFoot = currentPage + stepNum
                        End If
                        If (pageRoot <> 1) Then
                                If (currentPage > pageRoot) Then
                                        pageStr = pageStr & "<a href='" + UrlPrefix & "1' class=""first"">1 ...</a>"
                                End If
                                pageStr = pageStr & "<a href='" + UrlPrefix & "" & (currentPage - 1) & "' title='上页' class=""prev"">‹‹</a>"
                        End If
                        For i = pageRoot To pageFoot
                                If (i = currentPage) Then
                                        pageStr = pageStr & "<strong>" & i & "</strong>"
                                Else
                                        pageStr = pageStr & "<a href='" & UrlPrefix & "" & i & "'>" & i & "</a>"
                                End If
                                If (i = pageCount) Then
                                        Exit For
                                End If
                        Next
                        If (pageFoot <> pageCount) Then
                                pageStr = pageStr & "<a href='" & UrlPrefix & "" & (currentPage + 1) & "' title='下页' class=""next"">››</a>"
                                If (pageFoot > currentPage) Then
                                        pageStr = pageStr & "<a href='" & UrlPrefix & "" & pageCount & "' class=""last"">... " & pageCount & "</a>"
                                End If
                        End If
                        pageStr = pageStr & "<kbd><input type=""text"" name=""custompage"" size=""3"" nkeydown=""if(event.keyCode==13) {window.location='" & UrlPrefix & "'+this.value; return false;}"" /></kbd>"
                        pageStr = pageStr & "</div>"
                End If
                pageStr = pageStr & "<div style=""clear:both""></div>"
                pageStr = pageStr & "</div>"
                AutoGoPList = pageStr
        End Function

%>
C#

CODE:

       /// <summary>
        /// 职能分页
        /// </summary>
        /// <param name="countNum">记录数</param>
        /// <param name="currentPage">第几页</param>
        /// <returns></returns>
        public string AutoGoPList(int countNum, int PSize, int currentPage)
        {
            string UrlPrefix = GetUrlPrefix();
            int pageCount = countNum % PSize == 0 ? countNum / PSize : countNum / PSize + 1;
            currentPage = currentPage > pageCount ? pageCount : currentPage;
            currentPage = currentPage == 0 ? 1 : currentPage;
            int stepNum = 3;
            int pageRoot = 1;
            string pageStr = "<div class=\"p_btns\">";
            if (countNum > PSize)
            {
                pageCount = pageCount == 0 ? 1 : pageCount;
                currentPage = currentPage == 0 ? 1 : currentPage;

                pageStr += "<div class=\"p_bar\"><em> " + countNum.ToString() + " </em>";
                if (currentPage - stepNum < 2)
                    pageRoot = 1;
                else
                    pageRoot = currentPage - stepNum;
                int pageFoot = pageCount;
                if (currentPage + stepNum >= pageCount)
                    pageFoot = pageCount;
                else
                    pageFoot = currentPage + stepNum;
                if (pageRoot != 1)
                {
                    if (currentPage > pageRoot)
                        pageStr += "<a href='" + UrlPrefix + "1' class=\"first\">1 ...</a>";
                    pageStr += "<a href='" + UrlPrefix + "" + Convert.ToString(currentPage - 1) + "' title='上页' class=\"prev\">‹‹</a>";
                }
                for (int i = pageRoot; i <= pageFoot; i++)
                {
                    if (i == currentPage)
                    {
                        pageStr += "<strong>" + i.ToString() + "</strong>";
                    }
                    else
                    {
                        pageStr += "<a href='" + UrlPrefix + "" + i.ToString() + "'>" + i.ToString() + "</a>";
                    }
                    if (i == pageCount)
                        break;
                }
                if (pageFoot != pageCount)
                {
                    pageStr += "<a href='" + UrlPrefix + "" + Convert.ToString(currentPage + 1) + "' title='下页' class=\"next\">››</a>";
                    if (pageFoot > currentPage)
                        pageStr += "<a href='" + UrlPrefix + "" + pageCount.ToString() + "' class=\"last\">... " + pageCount.ToString() + "</a>";
                }
                pageStr += "<kbd><input type=\"text\" name=\"custompage\" size=\"3\" nkeydown=\"if(event.keyCode==13) {window.location='" + UrlPrefix + "'+this.value; return false;}\" /></kbd>";
                pageStr += "</div>";
            }
            pageStr += "<div style=\"clear:both\"></div>";
            pageStr += "</div>";
            return pageStr;
        }

        /// <summary>
        /// 获取当前地址前缀
        /// </summary>
        public string GetUrlPrefix()
        {
            string strUrl, result_url;
            strUrl = Request.ServerVariables["Url"];
            if (Request.QueryString != null) //如果有参数
            {
                if (Request.ServerVariables["Query_String"].Substring(0, 5) == "page=")//只有页参数
                {
                    result_url = strUrl + "?page=";
                }
                else
                {
                    string[] strUrl_left;
                    strUrl_left = Request.ServerVariables["Query_String"].Split(new string[] { "page=" }, StringSplitOptions.None);
                    if (strUrl_left.Length == 1)//没有页参数
                    {
                        result_url = strUrl + "?" + strUrl_left[0] + "&page=";
                    }
                    else
                    {
                        result_url = strUrl + "?" + strUrl_left[0] + "page=";
                    }
                }

            }
            else
            {
                result_url = strUrl + "?page=";
            }
            return result_url;
        }

TAG:

chy2z发布于2007-12-13 17:05:09

QUOTE:

决不放弃Delphi,ASP
勤奋学习ASP.NET
应该了解PHP
永不介入JSP
从未想象CGI
楼主的坚定信念,让人感慨!
随风缘的个人空间 随风缘 发布于2007-12-13 19:33:40
多谢二楼,哈!
我来说两句

(可选)