AspNetPager分页功能总结
下载AspNetPager.dll文件,放到项目bin文件夹中,并在项目中引用,然后添加到工具栏,可以实现拖拽控件了
拖拽AspNetPager到项目文件中,我的是用的repeater控件,拖拽到这个控件的底部,然后在aspx页面中,第二行代码中会多出
<%@ Register Assembly="AspNetPager" Namespace="Wuqi.Webdiyer" TagPrefix="webdiyer" %>
这么一句,然后在repeater控件下边会有一个
<webdiyer:AspNetPager ID="AspNetPager1" runat="server"> </webdiyer:AspNetPager>
对AspNetPager控件编辑其属性
<webdiyer:AspNetPager ID="PageCtrl" runat="server" PageSize="5" HorizontalAlign="Center" Width="100%" meta:resourceKey="AspNetPager1" Style="font-size: 10px" AlwaysShow="false" FirstPageText="首页" LastPageText="尾页" NextPageText="下一页" PrevPageText="上一页" SubmitButtonText="Go" SubmitButtonClass="submitBtn" CustomInfoStyle="font-size:14px;text-align:left;" InputBoxStyle="width:25px; border:1px solid #999999; text-align:center; " TextBeforeInputBox="转到第" TextAfterInputBox="页 " PageIndexBoxType="TextBox" ShowPageIndexBox="Always" TextAfterPageIndexBox="页" TextBeforePageIndexBox="转到" Font-Size="10px" CustomInfoHTML="共<font color='#ff0000'>%PageCount%</font>页,第<font color='#ff0000'>%CurrentPageIndex%</font>页" ShowCustomInfoSection="Left" CustomInfoSectionWidth="19%" PagingButtonSpacing="3px" onpagechanged="AspNetPager1_PageChanged"> </webdiyer:AspNetPager>
具体都是什么自己有时间去查吧我也不太了解
aspx页面文件基本搞定,后台代码中我把一些东西写到操作类中了
:取出每一页中的数据
/// <summary> /// 分页 /// </summary> /// <returns>返回DataTable数据集</returns> public DataTable SelectAll(int startIndex,int size) { DataTable dt = new DataTable(); string sql = @"select * from ( select *, ROW_NUMBER() OVER(Order by id asc ) AS RowNumber from category) b where RowNumber BETWEEN '" + startIndex + "' and '" + size + "' "; dt = sqlhelper.ExecuteQuery(sql,CommandType.Text); return dt; }
取出总的页数
/// <summary> /// 取出当前所有新闻分类总条数 /// </summary> /// <returns>返回DataTable数据集</returns> public int SelectCaCount() { int MyCount; string sql = @"select count(0) from category "; MyCount=new SQLHelper().GetRecordCount(sql); return MyCount; }
用到的sqlhelper类中的方法有:
/// <summary> /// ExecuteScalar--返回首行首列 /// </summary> /// <param name="XSqlString">sql语句</param> /// <returns>返回结果集的首行首列</returns> public int GetRecordCount(string XSqlString) { string SCount; SqlCommand Cmd = new SqlCommand(XSqlString, GetConn()); SCount = Cmd.ExecuteScalar().ToString().Trim(); //返回结果集的首行首列,忽略其它 if (SCount == "") SCount = "0"; return Convert.ToInt32(SCount); }
/// <summary> /// 执行带参数的增删改SQL语句或存储过程 /// </summary> /// <param name="sql">带参数的增删改SQL语句或存储过程</param> /// <param name="ct">命令类型</param> /// <returns></returns> public DataTable ExecuteQuery(string cmdText,CommandType ct) { DataTable dt = new DataTable(); cmd = new SqlCommand(cmdText, GetConn()); cmd.CommandType = ct; using (sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection)) { dt.Load(sdr); } return dt; }
public SQLHelper() { string connStr = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString; conn = new SqlConnection(connStr); } private SqlConnection GetConn() { if (conn.State==ConnectionState.Closed) { conn.Open(); } return conn; }