最近學校要做課題,閑來沒事研究了下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 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);
}
}
}
完成!!!