asp.net mysql 分页_Asp.Net真分页技术

最近学校要做课题,闲来没事研究了下Asp.net的分页,我使用Repeater进行数据的绑定,每次从数据库读取到8条数据填充到Repeater中,这样搞可以降低数据库的压力,提高效率.

效果图如下:

d6290c418d7a937b1c2a21bfd797f408.png

数据库设计如下:

77bd5ecff529c2cdd685197447d6799e.png

附加一下代码:

.pages {

color: #999;

overflow: auto;

}

.pages a, .pages .cpb {

text-decoration: none;

float: left;

padding: 0 5px;

border: 1px solid #ddd;

background: #ffff;

margin: 0 2px;

font-size: 17px;

color: #000;

}

.pages a:hover {

background-color: #347AB6;

color: #fff;

border: 1px solid #347AB6;

text-decoration: none;

}

.pages .cpb {

font-size:large;

font-weight: bold;

color: #fff;

background: #347AB6;

border: 1px solid #347AB6;

}

产品ID产品季度销售量操作

编辑

删除

取消

更新

CssClass="pages" CurrentPageButtonClass="cpb" PagingButtonSpacing="0" FirstPageText="首页"

LastPageText="尾页" NextPageText="后页" PrevPageText="前页" AlwaysShow="True"

NumericButtonCount="3" PageSize="5"

OnPageChanging="AspNetPager1_PageChanging1">

后台代码:

string strconn = System.Configuration.ConfigurationManager.ConnectionStrings["Mydb"].ToString();

private int id = 0; //保存指定行操作所在的ID号

protected void Page_Load(object sender, EventArgs e)

{

if (!Page.IsPostBack)

{

SqlConnection conn = new SqlConnection(strconn);

conn.Open();

SqlCommand cmd = new SqlCommand();

cmd.Connection = conn;

cmd.CommandText = "select count(*) from Tables";

AspNetPager1.AlwaysShow = true;

AspNetPager1.PageSize = 5;

AspNetPager1.RecordCount = (int)cmd.ExecuteScalar();

conn.Close();

this.DataBindToRepeater(0);//将数据绑定到Repeater控件上

}

}

private void DataBindToRepeater(int pageindex)

{

//使用using语句进行数据库连接

using (SqlConnection sqlCon = new SqlConnection(strconn))

{

sqlCon.Open(); //打开数据库连接

SqlCommand sqlcom = new SqlCommand(); //创建数据库命令对象

sqlcom.CommandText = "select * from(select ID as TableID,产品 as TableName,季度 as Tablejidu,销售量 as TableNumber from Tables)AS temp order by TableID offset "+pageindex*5+" rows fetch next 5 rows only"; //为命令对象指定执行语句

sqlcom.Connection = sqlCon; //为命令对象指定连接对象

this.Repeater1.DataSource = sqlcom.ExecuteReader(); //为Repeater对象指定数据源

this.Repeater1.DataBind(); //绑定数据源

}

}

protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)

{

//获取命令文本,判断发出的命令为何种类型,根据命令类型调用事件

if (e.CommandName == "Edit") //编辑命令

{

id = int.Parse(e.CommandArgument.ToString()); //获取命令ID号

}

else if (e.CommandName == "Cancel") //取消更新命令

{

id = -1;

}

else if (e.CommandName == "Delete") //删除行内容命令

{

id = int.Parse(e.CommandArgument.ToString()); //获取删除行的ID号

//删除选定的行,并重新指定绑定操作

this.DeleteRepeater(id);

}

else if (e.CommandName == "Update") //更新行内容命令

{

//获取更新行的内容和ID号

string strText = ((TextBox)e.Item.FindControl("txtTableName")).Text.Trim();

string jidu = ((TextBox)e.Item.FindControl("txtTablejidu")).Text.Trim();

string xiaoshou = ((TextBox)e.Item.FindControl("txtTableNumber")).Text.Trim();

int intId = int.Parse(((Label)e.Item.FindControl("Label5")).Text);

//更新Repeater控件的内容

this.UpdateRepeater(strText, intId,jidu,xiaoshou);

}

//重新绑定控件上的内容

this.DataBindToRepeater(0);

}

private void DeleteRepeater(int intId)

{

using (SqlConnection sqlCon = new SqlConnection(strconn))

{

sqlCon.Open(); //打开数据库连接

SqlCommand sqlcom = new SqlCommand(); //创建数据库命令对象

sqlcom.CommandText = "delete from Tables where id=@id"; //为命令对象指定执行语句

sqlcom.Connection = sqlCon; //为命令对象指定连接对象

//创建参数集合,并向sqlcom中添加参数集合

SqlParameter sqlParam = new SqlParameter("@id", intId);

sqlcom.Parameters.Add(sqlParam);

sqlcom.ExecuteNonQuery(); //指定更新语句

}

}

private void UpdateRepeater(string txtTableName, int intId,string jidu,string xiaoshou)

{

using (SqlConnection sqlCon = new SqlConnection(strconn))

{

sqlCon.Open(); //打开数据库连接

SqlCommand sqlcom = new SqlCommand(); //创建数据库命令对象

sqlcom.CommandText = "update Tables set 产品=@str,季度=@jidu,销售量=@xiaoshou where id=@id"; //为命令对象指定执行语句

sqlcom.Connection = sqlCon; //为命令对象指定连接对象

//创建参数集合,并向sqlcom中添加参数集合

SqlParameter[] sqlParam = {

new SqlParameter("@str", txtTableName),

new SqlParameter("@id", intId),

new SqlParameter("@jidu",jidu),

new SqlParameter("@xiaoshou",xiaoshou)

};

sqlcom.Parameters.AddRange(sqlParam);

sqlcom.ExecuteNonQuery(); //指定更新语句

}

}

protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)

{

//判断Repeater控件中的数据是否是绑定的数据源,如果是的话将会验证是否进行了编辑操作

//ListItemType 枚举表示在一个列表控件可以包括,例如 DataGrid、 DataList和 Repeater 控件的不同项目。

if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)

{

//获取绑定的数据源,这里要注意上面使用sqlReader的方法来绑定数据源,所以下面使用的DbDataRecord方法获取的

//如果绑定数据源是DataTable类型的使用下面的语句就会报错.

System.Data.Common.DbDataRecord record = (System.Data.Common.DbDataRecord)e.Item.DataItem;

//DataTable类型的数据源验证方式

//System.Data.DataRowView record = (DataRowView)e.Item.DataItem;

//判断数据源的id是否等于现在的id,如果相等的话证明现点击了编辑触发了userRepeat_ItemCommand事件

if (id == int.Parse(record["TableID"].ToString()))

{

((Panel)e.Item.FindControl("plItem")).Visible = false;

((Panel)e.Item.FindControl("plEdit")).Visible = true;

}

else

{

((Panel)e.Item.FindControl("plItem")).Visible = true;

((Panel)e.Item.FindControl("plEdit")).Visible = false;

}

}

}

protected void AspNetPager1_PageChanging1(object src, PageChangingEventArgs e)

{

AspNetPager1.CurrentPageIndex = e.NewPageIndex;

DataBindToRepeater(e.NewPageIndex-1);

}

}

}

完成!!!

41d449727d14bb0c28f697a4910c3d4a.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值