首页 > 编程语言 >webform asp.net gridview 分页 利用aspnetpager 分页

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

时间:2022-10-28 17:06:45浏览次数:53  
标签:asp 分页 System gridview GridView1 using null ds string


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

webform 哈哈。

 

这里温习下,gridview 真分页

 

webform asp.net gridview 分页 利用aspnetpager 分页_asp 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
}

 

标签:asp,分页,System,gridview,GridView1,using,null,ds,string
From: https://blog.51cto.com/51souta/5805147

相关文章