最近学校要做课题,闲来没事研究了下Asp.net的分页,我使用Repeater进行数据的绑定,每次从数据库读取到8条数据填充到Repeater中,这样搞可以降低数据库的压力,提高效率.
效果图如下:
数据库设计如下:
附加一下代码:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<link rel="stylesheet" href="layui.css" />
<link rel="stylesheet" href="bootstrap.css" />
<style type="text/css">
.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;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div style="width:500px;top:100px;">
<asp:Repeater ID="Repeater1" runat="server" OnItemCommand="Repeater1_ItemCommand" OnItemDataBound="Repeater1_ItemDataBound">
<HeaderTemplate>
<table class="layui-table">
<thead>
<tr>
<th>产品ID</th>
<th>产品</th>
<th>季度</th>
<th>销售量</th>
<th>操作</th>
</tr>
</thead>
<tbody>
</HeaderTemplate>
<ItemTemplate>
<asp:Panel ID="plItem" runat="server">
<tr>
<td>
<asp:Label ID="Label1" runat="server" Text=‘<%# Bind("TableID") %>‘></asp:Label>
</td>
<td>
<asp:Label ID="Label2" runat="server" Text=‘<%# Bind("TableName") %>‘></asp:Label>
</td>
<td>
<asp:Label ID="Label3" runat="server" Text=‘<%# Bind("Tablejidu") %>‘></asp:Label>
</td>
<td>
<asp:Label ID="Label4" runat="server" Text=‘<%# Bind("TableNumber") %>‘></asp:Label>
</td>
<td>
<asp:LinkButton ID="lbtEdit" class="layui-btn layui-btn-xs" CommandName="Edit" CommandArgument=‘<%#Eval("TableID") %>‘ runat="server">编辑</asp:LinkButton>
<asp:LinkButton ID="lbtDelete" class="layui-btn layui-btn-danger layui-btn-xs" CommandName="Delete" CommandArgument=‘<%#Eval("TableID") %>‘ runat="server">删除</asp:LinkButton>
</td>
</tr>
</asp:Panel>
<asp:Panel ID="plEdit" runat="server">
<tr>
<td>
<asp:Label ID="Label5" runat="server" Text=‘<%# Bind("TableID") %>‘></asp:Label>
</td>
<td>
<asp:TextBox ID="txtTableName" runat="server" Text=‘<%#Eval("TableName") %>‘></asp:TextBox>
</td>
<td>
<asp:TextBox ID="txtTablejidu" runat="server" Text=‘<%#Eval("Tablejidu") %>‘></asp:TextBox>
</td>
<td>
<asp:TextBox ID="txtTableNumber" runat="server" Text=‘<%#Eval("TableNumber") %>‘></asp:TextBox>
</td>
<td>
<asp:LinkButton ID="lbtCancel" class="layui-btn layui-btn-xs" CommandName="Cancel" CommandArgument=‘<%#Eval("TableID") %>‘ runat="server">取消</asp:LinkButton>
<asp:LinkButton ID="lbtUpdate" class="layui-btn layui-btn-xs" CommandName="Update" CommandArgument=‘<%#Eval("TableID") %>‘ runat="server">更新</asp:LinkButton>
</td>
</tr>
</asp:Panel>
</ItemTemplate>
<FooterTemplate>
</tbody></table>
</FooterTemplate>
</asp:Repeater>
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 30px;text-align:center">
<legend>
<webdiyer:AspNetPager ID="AspNetPager1" runat="server"
CssClass="pages" CurrentPageButtonClass="cpb" PagingButtonSpacing="0" FirstPageText="首页"
LastPageText="尾页" NextPageText="后页" PrevPageText="前页" AlwaysShow="True"
NumericButtonCount="3" PageSize="5"
OnPageChanging="AspNetPager1_PageChanging1">
</webdiyer:AspNetPager>
</legend>
</fieldset>
</div>
</form>
</body>
</html>
后台代码:
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 [email protected]"; //为命令对象指定执行语句
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 产品[email protected],季度[email protected],销售量[email protected] where [email protected]"; //为命令对象指定执行语句
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);
}
}
}
完成!!!
原文地址:https://www.cnblogs.com/wangxiaokui/p/11465255.html