Asp.Net真分页技术
<p>最近学校要做课题,闲来没事研究了下Asp.net的分页,我使用Repeater进行数据的绑定,每次从数据库读取到8条数据添补到Repeater中,这样搞可以低落数据库的压力,进步服从.</p><p>效果图如下:</p>
<p><div align="center"></div></p>
<p> </p>
<p> 数据库设计如下:</p>
<p><div align="center"></div></p>
<p> </p>
<p> 附加一下代码:</p>
<p><html xmlns="http://www.w3.org/1999/xhtml"><br /><head runat="server"><br /><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/><br /> <title></title><br /> <link rel="stylesheet" href="layui.css" /><br /> <link rel="stylesheet" href="bootstrap.css" /><br /> <style type="text/css"><br /> .pages {<br /> color: #999;<br /> overflow: auto;<br /> }</p>
<p> .pages a, .pages .cpb {<br /> <br /> text-decoration: none;<br /> float: left;<br /> padding: 0 5px;<br /> border: 1px solid #ddd;<br /> background: #ffff;<br /> margin: 0 2px;<br /> font-size: 17px;<br /> color: #000;<br /> }</p>
<p> .pages a:hover {<br /> <br /> background-color: #347AB6;<br /> color: #fff;<br /> border: 1px solid #347AB6;<br /> text-decoration: none;<br /> }</p>
<p> .pages .cpb {<br /> font-size:large;<br /> font-weight: bold;<br /> color: #fff;<br /> background: #347AB6;<br /> border: 1px solid #347AB6;<br /> }<br /> </style><br /></head><br /><body><br /> <form id="form1" runat="server"><br /> <div style="width:500px;top:100px;"><br /> <asp:Repeater ID="Repeater1" runat="server" OnItemCommand="Repeater1_ItemCommand" OnItemDataBound="Repeater1_ItemDataBound"><br /> <HeaderTemplate><br /> <table ><br /> <thead><br /> <tr><br /> <th>产物ID</th><br /> <th>产物</th><br /> <th>季度</th><br /> <th>销售量</th><br /> <th>操作</th><br /> </tr><br /> </thead><br /> <tbody><br /> </HeaderTemplate><br /> <ItemTemplate><br /> <asp:Panel ID="plItem" runat="server"><br /> <tr><br /> <td><br /> <asp:Label ID="Label1" runat="server" Text='<%# Bind("TableID") %>'></asp:Label><br /> </td><br /> <td><br /> <asp:Label ID="Label2" runat="server" Text='<%# Bind("TableName") %>'></asp:Label><br /> </td><br /> <td><br /> <asp:Label ID="Label3" runat="server" Text='<%# Bind("Tablejidu") %>'></asp:Label><br /> </td><br /> <td><br /> <asp:Label ID="Label4" runat="server" Text='<%# Bind("TableNumber") %>'></asp:Label><br /> </td><br /> <td><br /> <asp:LinkButton ID="lbtEdit" CommandName="Edit" CommandArgument='<%#Eval("TableID") %>' runat="server">编辑</asp:LinkButton><br /> <asp:LinkButton ID="lbtDelete"CommandName="Delete" CommandArgument='<%#Eval("TableID") %>' runat="server">删除</asp:LinkButton><br /> </td><br /> </tr><br /> </asp:Panel><br /> <asp:Panel ID="plEdit" runat="server"><br /> <tr><br /> <td><br /> <asp:Label ID="Label5" runat="server" Text='<%# Bind("TableID") %>'></asp:Label><br /> </td><br /> <td><br /> <asp:TextBox ID="txtTableName" runat="server" Text='<%#Eval("TableName") %>'></asp:TextBox><br /> </td><br /> <td><br /> <asp:TextBox ID="txtTablejidu" runat="server" Text='<%#Eval("Tablejidu") %>'></asp:TextBox><br /> </td><br /> <td><br /> <asp:TextBox ID="txtTableNumber" runat="server" Text='<%#Eval("TableNumber") %>'></asp:TextBox><br /> </td><br /> <td><br /> <asp:LinkButton ID="lbtCancel"CommandName="Cancel" CommandArgument='<%#Eval("TableID") %>' runat="server">取消</asp:LinkButton><br /> <asp:LinkButton ID="lbtUpdate"CommandName="Update" CommandArgument='<%#Eval("TableID") %>' runat="server">更新</asp:LinkButton><br /> </td><br /> </tr><br /> </asp:Panel><br /> </ItemTemplate><br /> <FooterTemplate><br /> </tbody></table><br /> </FooterTemplate><br /> </asp:Repeater><br /> <fieldsetstyle="margin-top: 30px;text-align:center"><br /> <legend><br /> <webdiyer:AspNetPager ID="AspNetPager1" runat="server"<br /> Css CurrentPageButton PagingButtonSpacing="0" FirstPageText="首页"<br /> LastPageText="尾页" NextPageText="后页" PrevPageText="前页" AlwaysShow="True"<br /> NumericButtonCount="3" PageSize="5"<br /> OnPageChanging="AspNetPager1_PageChanging1"><br /> </webdiyer:AspNetPager><br /> </legend><br /> </fieldset><br /> <br /> </div><br /> </form><br /></body><br /></html></p>
<p>后台代码:</p>
<p>string strconn = System.Configuration.ConfigurationManager.ConnectionStrings["Mydb"].ToString();<br /> private int id = 0; //生存指定行操作所在的ID号<br /> protected void Page_Load(object sender, EventArgs e)<br /> {<br /> if (!Page.IsPostBack)<br /> {<br /> SqlConnection conn = new SqlConnection(strconn);<br /> conn.Open();<br /> SqlCommand cmd = new SqlCommand();<br /> cmd.Connection = conn;<br /> cmd.CommandText = "select count(*) from Tables";<br /> AspNetPager1.AlwaysShow = true;<br /> AspNetPager1.PageSize = 5;<br /> AspNetPager1.RecordCount = (int)cmd.ExecuteScalar();<br /> conn.Close();<br /> this.DataBindToRepeater(0);//将数据绑定到Repeater控件上<br /> }<br /> }<br /> private void DataBindToRepeater(int pageindex)<br /> {<br /> //使用using语句进行数据库连接<br /> using (SqlConnection sqlCon = new SqlConnection(strconn))<br /> {<br /> sqlCon.Open();//打开数据库连接<br /> SqlCommand sqlcom = new SqlCommand(); //创建数据库命令对象<br /> sqlcom.CommandText = "select * from(select ID as TableID,产物 as TableName,季度 as Tablejidu,销售量 as TableNumber from Tables)AS temp order by TableIDoffset "+pageindex*5+" rows fetch next 5 rows only"; //为命令对象指定执行语句<br /> sqlcom.Connection = sqlCon; //为命令对象指定连接对象<br /> this.Repeater1.DataSource = sqlcom.ExecuteReader(); //为Repeater对象指定数据源<br /> this.Repeater1.DataBind(); //绑定数据源<br /> }<br /> }<br /> protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)<br /> {<br /> //获取命令文本,判定发出的命令为何种类型,根据命令类型调用变乱<br /> if (e.CommandName == "Edit")//编辑命令<br /> {<br /> id = int.Parse(e.CommandArgument.ToString()); //获取命令ID号<br /> }<br /> else if (e.CommandName == "Cancel") //取消更新命令<br /> {<br /> id = -1;<br /> }<br /> else if (e.CommandName == "Delete") //删除行内容命令<br /> {<br /> id = int.Parse(e.CommandArgument.ToString()); //获取删除行的ID号<br /> //删除选定的行,并重新指定绑定操作<br /> this.DeleteRepeater(id);<br /> }<br /> else if (e.CommandName == "Update") //更新行内容命令<br /> {<br /> //获取更新行的内容和ID号<br /> string strText = ((TextBox)e.Item.FindControl("txtTableName")).Text.Trim();<br /> string jidu = ((TextBox)e.Item.FindControl("txtTablejidu")).Text.Trim();<br /> string xiaoshou = ((TextBox)e.Item.FindControl("txtTableNumber")).Text.Trim();<br /> int intId = int.Parse(((Label)e.Item.FindControl("Label5")).Text);<br /> //更新Repeater控件的内容<br /> this.UpdateRepeater(strText, intId,jidu,xiaoshou);<br /> }</p>
<p> //重新绑定控件上的内容<br /> this.DataBindToRepeater(0);<br /> }<br /> private void DeleteRepeater(int intId)<br /> {<br /> using (SqlConnection sqlCon = new SqlConnection(strconn))<br /> {<br /> sqlCon.Open();//打开数据库连接</p>
<p> SqlCommand sqlcom = new SqlCommand(); //创建数据库命令对象<br /> sqlcom.CommandText = "delete from Tables where id=@id"; //为命令对象指定执行语句<br /> sqlcom.Connection = sqlCon; //为命令对象指定连接对象</p>
<p> //创建参数聚集,并向sqlcom中添加参数聚集<br /> SqlParameter sqlParam = new SqlParameter("@id", intId);<br /> sqlcom.Parameters.Add(sqlParam);</p>
<p> sqlcom.ExecuteNonQuery(); //指定更新语句</p>
<p> }<br /> }<br /> private void UpdateRepeater(string txtTableName, int intId,string jidu,string xiaoshou)<br /> {<br /> using (SqlConnection sqlCon = new SqlConnection(strconn))<br /> {<br /> sqlCon.Open();//打开数据库连接<br /> SqlCommand sqlcom = new SqlCommand(); //创建数据库命令对象<br /> sqlcom.CommandText = "update Tables set 产物=@str,季度=@jidu,销售量=@xiaoshou where id=@id"; //为命令对象指定执行语句<br /> sqlcom.Connection = sqlCon; //为命令对象指定连接对象<br /> //创建参数聚集,并向sqlcom中添加参数聚集<br /> SqlParameter[] sqlParam = { <br /> new SqlParameter("@str", txtTableName), <br /> new SqlParameter("@id", intId),<br /> new SqlParameter("@jidu",jidu),<br /> new SqlParameter("@xiaoshou",xiaoshou)<br /> };<br /> sqlcom.Parameters.AddRange(sqlParam);<br /> sqlcom.ExecuteNonQuery(); //指定更新语句<br /> }<br /> }<br /> protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)<br /> {<br /> //判定Repeater控件中的数据是否是绑定的数据源,如果是的话将会验证是否进行了编辑操作<br /> //ListItemType 枚举表示在一个列表控件可以包罗,比方 DataGrid、 DataList和 Repeater 控件的差别项目。 <br /> if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)<br /> {<br /> //获取绑定的数据源,这里要注意上面使用sqlReader的方法来绑定数据源,所以下面使用的DbDataRecord方法获取的<br /> //如果绑定数据源是DataTable类型的使用下面的语句就会报错.<br /> System.Data.Common.DbDataRecord record = (System.Data.Common.DbDataRecord)e.Item.DataItem;<br /> //DataTable类型的数据源验证方式<br /> //System.Data.DataRowView record = (DataRowView)e.Item.DataItem;</p>
<p> //判定数据源的id是否即是如今的id,如果相称的话证实现点击了编辑触发了userRepeat_ItemCommand变乱<br /> if (id == int.Parse(record["TableID"].ToString()))<br /> {<br /> ((Panel)e.Item.FindControl("plItem")).Visible = false;<br /> ((Panel)e.Item.FindControl("plEdit")).Visible = true;<br /> }<br /> else<br /> {<br /> ((Panel)e.Item.FindControl("plItem")).Visible = true;<br /> ((Panel)e.Item.FindControl("plEdit")).Visible = false;<br /> }<br /> }<br /> }</p>
<p> protected void AspNetPager1_PageChanging1(object src, PageChangingEventArgs e)<br /> {<br /> AspNetPager1.CurrentPageIndex = e.NewPageIndex;<br /> DataBindToRepeater(e.NewPageIndex-1);<br /> }<br /> }<br />}</p>
<p>完成!!!</p>
<p><div align="center"></div></p>
<p> </p><br><br/><br/><br/><br/><br/>来源:<a href="https://www.cnblogs.com/wangxiaokui/p/11465255.html" target="_blank">https://www.cnblogs.com/wangxiaokui/p/11465255.html</a>
页:
[1]