天天看點

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

最近做項目成功溫習了一個10年前的老項目asp.net

webform 哈哈。

這裡溫習下,gridview 真分頁

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

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
}