请选择 进入手机版 | 继续访问电脑版

马上加入IBC程序猿 各种源码随意下,各种教程随便看! 注册 每日签到 加入编程讨论群

C#教程 ASP.NET教程 C#视频教程程序源码享受不尽 C#技术求助 ASP.NET技术求助

【源码下载】 社群合作 申请版主 程序开发 【远程协助】 每天乐一乐 每日签到 【承接外包项目】 面试-葵花宝典下载

官方一群:

官方二群:

Asp.Net真分页技术

[复制链接]
查看1963 | 回复0 | 2019-9-12 16:19:43 | 显示全部楼层 |阅读模式

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

效果图如下:

162344orl1lodlsa5asol0.png

数据库设计如下:

162345edo1srx5oxff5gd4.png

附加一下代码:















































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









编辑
删除









取消
更新





Css CurrentPageButton 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);
}
}
}

完成!!!

162345z1bng6pcmqb23gzd.png







来源:https://www.cnblogs.com/wangxiaokui/p/11465255.html
C#论坛 www.ibcibc.com IBC编程社区
C#
C#论坛
IBC编程社区
*滑块验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则