准备:
GridView ,AspnetPager 控件。
原理:
首先在CS文件写一个绑定GridView的方法。 在页面的pageload事件中调用此方法!再当每次点击AspnetPager的时候进行分页。
页面代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Pager.aspx.cs" Inherits="Pager_" %>
<%@ Register Assembly="AspNetPager" Namespace="Aspnet.Webdiyer" TagPrefix="Asp" %>
<!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 id="Head1" runat="server">
<title>销售部门管理</title>
<style type="text/css">
.style1
{
width: 100%;
}
#Title2
{
width: 256px;
}
#spec
{
width: 257px;
}
#Title3
{
width: 234px;
}
#spec0
{
width: 257px;
}
#Title4
{
width: 234px;
}
#spec1
{
width: 257px;
}
#Title5
{
width: 256px;
}
#spec2
{
width: 230px;
}
#Select1
{
width: 149px;
}
#txtProduct
{
width: 589px;
}
.style2
{
width: 13%;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
</table>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<td align="center" valign="top"> </td>
</tr>
<tr>
<td width="100%" align="center" valign="top">
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0" class="border">
<tr align="center">
<td class="tdbg"> <table width="100%" border="0" cellpadding="2" cellspacing="1" class="table_southidc">
<tr>
<td class="back_southidc" height="22" colspan="2" align="right" bgcolor="#A4B6D7"><div align="center"><b><font color="#ffffff">销 售 部 门
管 理</font></b></div></td>
</tr>
<tr>
<td colspan="2">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
Width="90%" DataKeyNames="X_id" onrowdeleting="GridView1_RowDeleting">
<Columns>
<asp:BoundField DataField="X_type" HeaderText="销售部门" />
<asp:BoundField DataField="X_detail" HeaderText="电话" />
<asp:BoundField DataField="X_name" HeaderText="姓名" />
<asp:BoundField DataField="X_phone" HeaderText="手机" />
<asp:CommandField HeaderText="删除用户" ShowDeleteButton="True" />
</Columns>
</asp:GridView>
</td>
</tr>
<tr>
<td colspan="2">
<Asp:AspNetPager ID="AspNetPager1" runat="server" PageSize="5" AlwaysShow="true"
FirstPageText="第一页 " LastPageText=" 最后一页" ShowBoxThreshold="5"
PrevPageText=" 上一页 " NextPageText=" 下一页 " onpagechanged="AspNetPager1_PageChanged"
NumericButtonTextFormatString="[{0}]"
>
</webdiyer:AspNetPager>
</td>
</tr>
<tr><td> </td></tr>
</table></td>
</tr>
</table></td></tr>
</table>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
后台代码:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using XWE.Model; using XWE.BLL; using System.Data; public partial class admin_addxsbm : System.Web.UI.Page { static int count = 0;//保存数据总数量,用于计算分页 XsbmModel xsbmModel = new XsbmModel(); XsbmSystem xsbmSystem = new XsbmSystem(); protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { getGridView();// 绑定GridView AspNetPager1.RecordCount = count; } } /// <summary> /// 提交 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void Button1_Click(object sender, EventArgs e) { xsbmModel.X_type=txtType.Text; xsbmModel.X_detail = txtDetail.Text; xsbmModel.X_name = this.TextBox1.Text; xsbmModel.X_phone = this.TextBox2.Text; bool flag= xsbmSystem.getAddXsbm(xsbmModel); if (flag) { Response.Write("<script>alert('添加成功');</script>"); } Response.Redirect("addxsbm.aspx"); } /// <summary> /// 绑定GridView /// </summary> private void getGridView() { int start = AspNetPager1.PageSize * (AspNetPager1.CurrentPageIndex - 1); int pagesize = AspNetPager1.PageSize; IList<XsbmModel> list = xsbmSystem.GetList(pagesize, start); count = xsbmSystem.getCount(); GridView1.DataSource = list; GridView1.DataBind(); AspNetPager1.CustomInfoText = "记录总数:<b>" + AspNetPager1.RecordCount.ToString() + "</b>"; AspNetPager1.CustomInfoText += " 总页数:<b>" + AspNetPager1.PageCount.ToString() + "</b>"; AspNetPager1.CustomInfoText += " 当前页:<font color=\"red\"><b>" + AspNetPager1.CurrentPageIndex.ToString() + "</b></font>"; } /// <summary> /// 分页 /// </summary> /// <param name="src"></param> /// <param name="e"></param> protected void AspNetPager1_PageChanged(object src, Asp.Webdiyer.PageChangedEventArgs e) { AspNetPager1.CurrentPageIndex = e.NewPageIndex; getGridView(); } }
AspnetPager 属性:
1、CustomInfoSectionWidth 用户自定义信息区的宽度。
2、InvalidPageIndexErrorString 用户输入无效的页索引时在客户端显示的错误信息。
3、NumericButtonTextFormatString 页索引数值按钮上文字的显示格式。
4、PageIndexOutOfRangeErroString 当用户输入的页索引超出范围时在客户端显示的错误信息。
5、PageSize 每页显示的记录数。
6、NumericbuttonCount 要显示的页索引值按钮的数目。