控件类代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Reflection;
using System.IO;
[assembly: WebResource("PageBarJS.js", "application/x-javascript")]
namespace Hawkon.Control {
[DefaultProperty("Text")]
[ToolboxData("<{0}:PageBar runat=server>{0}:PageBar>")]
public class PageBar : WebControl {
[Bindable(true)]
[Category("Data")]
[DefaultValue("1")]
[Localizable(true)]
public int PageIndex {
get {
return pageIndex;
}
set {
pageIndex = value;
}
}
private int pageIndex;
[Bindable(true)]
[Category("Data")]
[DefaultValue("1")]
[Localizable(true)]
public int PageCount {
get {
return pageCount;
}
set {
pageCount = value;
}
}
private int pageCount;
[Bindable(true)]
[Category("Data")]
[DefaultValue("5")]
[Localizable(true)]
public int DisplayCount {
get {
return displayCount;
}
set {
displayCount = value;
}
}
private int displayCount;
protected override void RenderContents(HtmlTextWriter output) {
string html = "";
html = CreateA(1, "<<");
int b = 0, e = 0;
if (pageIndex <= displayCount) {
b = 1;
e = displayCount * 2 1;
}
else if (pageIndex > pageCount - displayCount) {
b = pageCount - displayCount * 2;
e = pageCount;
}
else {
b = pageIndex - displayCount;
e = pageIndex displayCount;
}
if (b <= 1) {
html = CreateA(1, "");
}
else
html = CreateA(b - 1, "");
for (int i = b; i <= e; i ) {
html = CreateA(i, i.ToString());
}
html = CreateA(e 1, "");
html = CreateA(pageCount, ">>");
// html = string.Format("",
// this.Page.ClientScript.GetWebResourceUrl(typeof(PageBar), "JScript1.js"));
output.Write(html);
}
private string CreateA(int pageIndex, string text) {
if (pageIndex == this.pageIndex) {
return string.Format("
{1} ", pageIndex, text);
}
return string.Format("
{1} ", pageIndex, text);
}
protected override void OnPreRender(EventArgs e) {
base.OnPreRender(e);
string resourceName = "PageBarJS.js";
string url = this.Page.ClientScript.GetWebResourceUrl(this.GetType(), "PageBarJS.js");
string script = "rn";
this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), resourceName, script, false);
script = @""; this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "ready", script);
}
protected override void Render(HtmlTextWriter writer) {
base.Render(writer);
}
}
}
JS资源文件代码:
var displayCount;
var getDataUrl;
var bookTableId;
var currentIndex;
var pageCount;
var linkClass;
var fields;
function InitPageBar(dc, gdu, btId,pc,lc) {
displayCount = dc;
getDataUrl = gdu;
bookTableId = btId;
currentIndex = 1;
pageCount = pc;
linkClass = "." lc;
$(linkClass).click(GetPage);
}
function GetPageById(id) {
$("#CI").val(id);
var surl = getDataUrl "?pageIndex=" id;
$.ajax({
url: surl,
type: "GET",
dataType: "json",
timeout: 1000,
success: showResult
}
);
}
function GetPage() {
GetPageById($(this).get(0).id);
}
function showResult(result) {
for (i = 1; i <= result.Data.length; i ) {
var id = "#" bookTableId " tr:nth-child(" i ")";
obj = result.Data[i - 1];
for (var key in obj) {
ctl = $(id).find("." key);
if (ctl.length > 0) {
ctl.get(0).innerHTML = obj[key];
}
}
}
$(linkClass).each(function(index) {
var i, b, e;
if (result.CurrentPageIndex <= displayCount) {
b = 1;
e = (displayCount 1) * 2;
i = index - 2 1;
}
else if (result.CurrentPageIndex > pageCount - displayCount) {
b = pageCount - displayCount * 2;
e = pageCount;
i = pageCount - displayCount * 2 index - 2;
}
else {
i = result.CurrentPageIndex - displayCount index - 2;
b = result.CurrentPageIndex - displayCount - 1;
e = result.CurrentPageIndex displayCount 1;
}
if ($(this).get(0).id == $(this).text()) {
$(this).text(i);
}
else if (index == 1) {
if (b <= 1) {
$(this).get(0).id = 1;
}
else {
$(this).get(0).id = b - 1;
}
}
else if (index == displayCount * 2 3) {
$(this).get(0).id = e;
}
$(this).attr("href", "#");
if ((i >= b) && (i <= e)) {
$(this).get(0).id = i;
}
if ($(this).text == result.CurrentPageIndex) {
$(this).removeAttr("href");
}
});
currentIndex = result.CurrentPageIndex;
}
HTML页面代码:
Ajax请求地址返回的JSON数据:
{"Data":[{"Title":"C#.NET Programming - Foreign Computer Science Classic Textbook","CategoryName":".NET","Author":"Bradley (Bradley, J.C.), written by Millspaugh, A.C., translated by Tianhong Studio","Id":5392},{"Title":"C#2.0 (Bible) (attached)","CategoryName" :"C C VC VC ","Author":"Compiled by Zhang Li","Id":5027},{"Title":"C#2.0 Complete Self-Study Manual (with CD)","CategoryName":"C C VC VC ","Author":"Compiled by Zhang Li et al.","Id":5081},{"Title":"C#2005 Database Programming Classic Tutorial","CategoryName":"C C VC VC ","Author" :" (U.S.) Written by Karli watton, translated by Chen Qiuping","Id":4983},{"Title":"C# Programming Tutorial - Computer Basics Course Series Textbooks","CategoryName":"C C VC VC " ,"Author":"Zheng Aqi, Chief Editor Liang Jingdong","Id":5127},{"Title":"C# Programmer Reference Manual","CategoryName":"C C VC VC ","Author":"Grant Written by Palmer, translated by Kangbo","Id":5132},{"Title":"C# and .NET Core Technology - Developer Professional Technology Series","CategoryName":"C C VC VC ","Author":" (U.S.A.) Written by Perry, S.C., translated by Xiao Bin and others","Id":5104},{"Title":"C# Basics and Examples Tutorial (with a CD-ROM)","CategoryName" :" C C VC VC ","Author":"Compiled by Hao Chunqiang","Id":5071},{"Title":"Analysis of the whole process of C# software project development","CategoryName":"C C VC VC ","Author ":" (Germany) Houm, (Germany) Kruger, (Germany) Spuda, translated by Xue Xingtao, Yuan Qinyong","Id":5034},{"Title":"C# Design Pattern - Development Master Series", "CategoryName":"C C VC VC ","Author":"(U.S.) Mesko, translated by Yan Jiong","Id":4954}],"CurrentPageIndex":15,"PageCount":1074 }