zoukankan      html  css  js  c++  java
  • 一个分页页码函数

    说明下:参数PageIndex是从0开始;参数PageCount表示需要分页的数量;
    using
    System;
    using System.Collections.Generic;

    public class MyClass
    {
    static int startIndex,endIndex;
    static int pageSize=4;
    static int pageCount=13;
    public static void RunSnippet()
    {
    for(int i=0;i<21;i++)
    g(i);
    }
    static void g(int pageIndex)
    {
    GetPageArea(ref startIndex,ref endIndex,pageIndex,pageSize,pageCount);
    WL("Page{2}\tArea is : \t{0} \t-- \t{1}",startIndex,endIndex,pageIndex);
    }
    static void GetPageArea(ref int startIndex, ref int endIndex, int pageIndex, int pageSize, int pageCount)
    {
    if ( pageSize < 0 || pageCount < 0)
    throw new ArgumentException("Maybe args too big!");

    if (pageSize == 0 || pageCount == 0)
    {
    startIndex = 0;
    endIndex = 0;
    return;
    }

    if (pageIndex <= pageSize/2)
    {
    startIndex = 0;
    endIndex = pageSize - 1;
    return;
    }
    if (pageIndex > pageCount-pageSize/2-1)
    {
    startIndex = pageCount - pageSize;
    endIndex = pageCount - 1;
    return;
    }

    startIndex = pageIndex - pageSize / 2;
    endIndex = pageIndex + pageSize / 2 - (pageSize % 2 == 1 ? 0 : 1);
    }
    #region Helper methods

    public static void Main()
    {
    try { RunSnippet(); } catch (Exception e)
    {
    string error = string.Format("---\nThe following error occurred while executing the snippet:\n{0}\n---", e.ToString());
    Console.WriteLine(error);
    }
    finally { Console.Write("Press any key to continue...");
    Console.ReadKey();
    }
    }

    private static void WL(object text, params object[] args)
    {
    Console.WriteLine(text.ToString(), args);
    }

    private static void RL()
    {
    Console.ReadLine();
    }

    private static void Break()
    {
    System.Diagnostics.Debugger.Break();
    }

    #endregion }
    作者:KKcat
        
    个人博客:http://jinzhao.me/
        
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    Cookie练习
    JS写九九乘法表
    对GridView实现分页
    对GridView的行加颜色并弹出Kindeditor
    对Dictionary的理解
    一、android 开发环境大搭建
    main方法的测试
    main 方法的书写(1)
    由InvocationTargetException引发的思考
    汇编学习笔记之处理器体系结构
  • 原文地址:https://www.cnblogs.com/jinzhao/p/1398196.html
Copyright © 2011-2022 走看看