webform asp.net gridview 分页 利用aspnetpager 分页

最近做项目成功温习了一个10年前的老项目asp.net

webform 哈哈。

 

这里温习下,gridview 真分页

 

1. BindDataPage 真分页

2. BindData 假分页

 

都给了相应的列子 大家可以试试

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using XSqlHelper;

public partial class test1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack) {
            BindDataPage();
        }
       // BindDataPage();
    }

   
 
    private void BindDataPage() {      
        int pageIndex = this.AspNetPager1.CurrentPageIndex;
        PageBySelect(pageIndex);
        this.AspNetPager1.RecordCount = GetAllCount();//总数
    }

    /// <summary>
    /// 传入要查询的页数
    /// </summary>
    /// <param name="pageIndex"></param>
    /// <returns></returns>
    private DataTable PageBySelect(int pageIndex)
    {
        string strConn = publics.ConnectionString;
        int count = 10 * (pageIndex - 1);
        string strSQL = "select top 10 * from  dbo.ElectronicFence_Alarm  where id not in( select top "+ count + " id from ElectronicFence_Alarm order by id)";

        SqlHelper sqlHelper = new SqlHelper(strConn, CommandType.Text, strSQL, null);
        DataSet ds = sqlHelper.ReturnDataSet();
        if (ds != null)
        {
            if (ds.Tables.Count > 0)
            {
                GridView1.RowStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center;
                GridView1.DataSource = ds.Tables[0];
                GridView1.DataBind();
            }
        }
        return null;
    }

    /// <summary>
    /// 拿到总条数
    /// </summary>
    /// <returns></returns>
    private int GetAllCount() {
        string strConn = publics.ConnectionString;
        string strSQL = "select COUNT(*) as allCount from ElectronicFence_Alarm";
        SqlHelper sqlHelper = new SqlHelper(strConn, CommandType.Text, strSQL, null);
        DataSet ds = sqlHelper.ReturnDataSet();
        if (ds != null)
        {
            if (ds.Tables.Count > 0)
            {              
                DataTable dataTableAllCount= ds.Tables[0];
                foreach (DataRow dataRow in dataTableAllCount.Rows)
                {
                    string allCountStr = dataRow["allCount"].ToString();
                    return int.Parse(allCountStr);
                }
            }
        }
        return 0;
    }

    /// <summary>
    /// 点击改变分页事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        BindDataPage();
    }


    # region 假分页 数据量大不要用

    private void BindData()
    {

        string strConn = publics.ConnectionString;
        string strSQL = "select id,cardId,createtime from ElectronicFence_Alarm";
        SqlHelper sqlHelper = new SqlHelper(strConn, CommandType.Text, strSQL, null);
        DataSet ds = sqlHelper.ReturnDataSet();
        if (ds != null)
        {
            if (ds.Tables.Count > 0)
            {
                GridView1.RowStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center;
                GridView1.DataSource = ds.Tables[0];
                GridView1.DataBind();
            }
        }
    }


    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        BindData();
    }
    #endregion
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

霍先生的虚拟宇宙网络

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值