AspnetPager 分页

本文介绍了一个使用ASP.NET实现的简单分页示例。通过GridView和AspNetPager控件展示了如何进行数据绑定及分页操作,并提供了完整的页面代码和后台逻辑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

准备:

       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>
&nbsp;<table width="100%" border="0" cellspacing="0" cellpadding="0">

<td align="center" valign="top">&nbsp;</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">&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;</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>&nbsp;</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 用户自定义信息区的宽度。

  2011021509441618.png

      2、InvalidPageIndexErrorString 用户输入无效的页索引时在客户端显示的错误信息。

2011021509443543.png

      3、NumericButtonTextFormatString 页索引数值按钮上文字的显示格式。

2011021509445112.png

      4、PageIndexOutOfRangeErroString 当用户输入的页索引超出范围时在客户端显示的错误信息。

2011021509451548.png

  5、PageSize  每页显示的记录数。

2011021509453044.png

      6、NumericbuttonCount 要显示的页索引值按钮的数目。

2011021509454711.png

转载于:https://www.cnblogs.com/307914070/archive/2011/02/15/1954845.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值