62,267
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Maticsoft.Web
{
public partial class WebForm2 : System.Web.UI.Page
{
//第几页。默认值为 1。
public int PageIndex
{
get
{
return ViewState["PageIndex"] == null ? 1 : (int)ViewState["PageIndex"];
}
set
{
ViewState["PageIndex"] = value;
}
}
//共几页。默认值为 50。
public int PageCount
{
get
{
return ViewState["PageCount"] == null ? 50 : (int)ViewState["PageCount"];
}
set
{
ViewState["PageCount"] = value;
}
}
protected void Page_Load(object sender, EventArgs e)
{
setPagingNum();
}
protected void lbPaging_Click(object sender, CommandEventArgs e)
{
string Argument = e.CommandArgument.ToString();//获取翻页类型
switch (Argument)
{
default: PageIndex = int.Parse(Argument); break;
}
setPagingNum();
}
private void setPagingNum()
{
int numlkCount = 5;//预定义数字页码数量
Panel1.Controls.Clear();//清空所有数字按钮
int begin = PageIndex - numlkCount / 2;//计算起始页码
if (begin < 1) begin = 1;
int end = begin + numlkCount - 1;//计算结束页码
if (end > PageCount) end = PageCount;
for (int i = begin; i <= end; i++)
{
LinkButton lb = new LinkButton();
lb.Command += lbPaging_Click;//为按钮添加翻页事件
lb.CommandArgument = i.ToString();
lb.Text = "[" + i.ToString() + "] ";
Panel1.Controls.Add(lb);//添加数字页码按钮到页面
}
}
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="Maticsoft.Web.WebForm2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Panel ID="Panel1" runat="server">
</asp:Panel>
</div>
</form>
</body>
</html>