C#_简单实用的翻页

简单实用的生成翻页HTML辅助类

C#

 using System.Text;
namespace ClassLibrary
{
/// <summary>
///
/// </summary>
public class PageTurning
{
#region 构造函数
/// <summary>
/// 翻页
/// </summary>
/// <param name="count">页码总量</param>
/// <param name="showCount">显示数量</param>
/// <param name="index">当前页码</param>
/// <param name="element">显示元素</param>
/// <param name="css">默认样式</param>
/// <param name="cssChecked">选中样式</param>
/// <param name="cssPageTurning">上一页下一页样式</param>
/// <param name="click">翻页事件</param>
public PageTurning(int count, int showCount, int index, string element, string css, string cssChecked, string cssPageTurning, string click)
{
this.Count = count;
this.ShowCount = showCount;
this.Index = index;
this.Element = element;
this.Css = css;
this.CssChecked = cssChecked;
this.CssPageTurning = cssPageTurning;
this.Click = click;
}
#endregion #region 参数
#region 基本参数
/// <summary>
/// 当前页码
/// </summary>
public int Index { get; set; }
/// <summary>
/// 页码总量
/// </summary>
public int Count { get; set; }
/// <summary>
/// 显示数量
/// </summary>
public int ShowCount { get; set; }
#endregion #region HTML参数
/// <summary>
/// 显示元素
/// </summary>
public string Element { get; set; }
/// <summary>
/// 默认样式
/// </summary>
public string Css { get; set; }
/// <summary>
/// 选中样式
/// </summary>
public string CssChecked { get; set; }
/// <summary>
/// 上一页下一页样式
/// </summary>
public string CssPageTurning { get; set; }
/// <summary>
/// 翻页事件
/// </summary>
public string Click { get; set; }
#endregion
#endregion #region 生成HTML #region 获取最大最小值
/// <summary>
/// 获取最大最小值
/// </summary>
/// <returns></returns>
private int[] GetMinMax()
{
int[] minMax = new int[];
if (Index < ShowCount + )//123...
{
minMax[] = ;
minMax[] = Count > (ShowCount * + ) ? (ShowCount * + ) : Count;
}
else if (Index > Count - * ShowCount + )//...678
{
minMax[] = Count - * ShowCount;
minMax[] = Count;
}
else//...345...
{
minMax[] = Index - ShowCount;
minMax[] = Index + ShowCount;
}
//防止出错
minMax[] = minMax[] < ? : minMax[];
minMax[] = minMax[] < Count ? minMax[] : Count;
minMax[]++;
return minMax;
}
#endregion #region 生成HTML
/// <summary>
/// 生成HTML
/// </summary>
/// <returns></returns>
public string Building()
{
var minMax = GetMinMax();
StringBuilder sb = new StringBuilder();
if (Index > )//加1...
{
sb.AppendFormat("<{0} class={1} onclick={2}>上一页</{0}>", Element, CssPageTurning, Click + "(" + (Index > ? Index - : ) + ")");
if (minMax[] > )
{
sb.AppendFormat("<{0} class={1} onclick={2}>1</{0}>", Element, Css, Click + "(1)");
if (minMax[] > )
{
sb.AppendFormat("<{0} class={1}>...</{0}>", Element, Css);
}
}
}
while (minMax[] < minMax[])//1...345...7
{
sb.AppendFormat("<{0} class={1} onclick={2}>{3}</{0}>", Element, minMax[] != Index ? Css : CssChecked, minMax[] != Index ? Click + "(" + minMax[] + ")" : "", minMax[]);
minMax[]++;
}
if (Index < Count)//加..7
{
if (minMax[] < Count + )
{
if (minMax[] < Count)
{
sb.AppendFormat("<{0} class={1}>...</{0}>", Element, Css);
}
sb.AppendFormat("<{0} class={1} onclick={2}>{3}</{0}>", Element, Css, Click + "(" + Count + ")", Count);
}
sb.AppendFormat("<{0} class={1} onclick={2}>下一页</{0}>", Element, CssPageTurning, Click + "(" + (Count > Index ? Index + : Count) + ")");
}
return sb.ToString();
}
#endregion
#endregion
}
}

调用

public string get(int index)
{
  PageTurning pt = new PageTurning(20,1,index,"a","aa","bb","cc","aaaaa");
  return pt.Building();
}

HTML实例

 <!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
<script src="~/Scripts/jquery-1.8.2.min.js"></script>
<script>
$(function () {
aaaaa(22)
})
function aaaaa(pIndex) {
$("div").load("/test/get", { index: pIndex });
}
</script>
<style>
.aa {
cursor: pointer;
display: block;
border: 1px solid black;
width: 22px;
height: 24px;
float: left;
margin-left: 4px;
} .bb {
cursor: pointer;
display: block;
border: 1px solid black;
width: 22px;
height: 24px;
float: left;
margin-left: 4px;
background-color: pink;
} .cc {
cursor: pointer;
display: block;
border: 1px solid black;
width: 60px;
height: 24px;
float: left;
margin-left: 4px;
background-color: orange;
}
</style>
</head>
<body>
<div></div>
</body>
</html>

效果图

C#_简单实用的翻页

上一篇:递推DP 赛码 1005 Game


下一篇:简单实用的Windows命令(二)