.
NET中关于 中关于GridView、Repeater(Datalist)分页
问题(验证过) 验证过) 中关于 、 ( )
GridView、Repeater(Datalist)所用到的公共类 、 ( )
using System; using System.Data.SqlClient; using System.Configuration; using System.Data; namespace SqlHelps { public abstract class SqlHelper { private static string connection_string = ""; ///
/// 数簓据库连接 /// private static SqlConnection sql_connect; static SqlHelper() { ConnectionString ConfigurationManager.ConnectionStrings["ConnectionString"].ToString(); } ~SqlHelper() { Dispose(); } private static void InitializeConnection() { sql_connect } public static string ConnectionString { set { connection_string = value; InitializeConnection(); } get { return connection_string; } } private static void Open() { = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString()); =
sql_connect.Open(); } public static void Close() { if (sql_connect != null) sql_connect.Close(); } public static void Dispose() { if (sql_connect != null) { sql_connect.Dispose();
sql_connect = null; } } ///
/// 查询数据库并返回SqlDataReader /// ///
查询时所用的SQL 语句 ///
返回查询结果的数据集 public static SqlDataReader Query(string sql_str) { InitializeConnection(); try { Close(); SqlCommand cmd = new SqlCommand(); cmd.Connection = sql_connect; cmd.CommandText = sql_str; Open(); return cmd.ExecuteReader(); } catch (Exception e) { throw new Exception("执行SQL 查询语句时出错 } } ///
/// 查询数据库并返回DataSet /// 查询时骸 /// 所用的SQL 语句 :" + e.Message);
///
返回查询结果的数据集 public static DataSet Duery(string sql_str)
{ InitializeConnection(); try { Close(); SqlDataAdapter mydap = new SqlDataAdapter(sql_str, sql_connect); DataSet ds = new DataSet(); mydap.Fill(ds); return ds; } catch (Exception e) { throw new Exception("执行SQL 查询语句时出错 } } } } " + e.Message);
GridView (1)在aspx中:
xml:namespace prefix = asp />
自己需要什么自己填写 //分页样式 首 页&;nbsp;
CommandArgument="Prev" CommandName="Page">上一页 &;nbsp; CommandArgument="Next" CommandName="Page">下一页 &;nbsp; CommandArgument="Last" CommandName="Page"> 尾 页 &;nbsp; 第 runat="serve
r" 页 共 %>"> Text="<%#((GridView)Container.Parent.Parent).PageIndex
((GridView)Container.Parent.Parent).PageCount %>"> &;nbsp;页 跳到 GO 样式如图:
(2)在aspx.cs中:
using System; using System.Web.UI.WebControls; using System.Data.SqlClient; protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) { GridView theGrid = sender as GridView; int newPageIndex = 0; if (e.NewPageIndex == -3) { TextBox txtNewPageIndex = null; GridViewRow pagerRow = theGrid.BottomPagerRow; if (pagerRow != null) { txtNewPageIndex = pagerRow.FindControl("txtNewPageIndex") as TextBox; } if (txtNewPageIndex != null) { newPageIndex = int.Parse(txtNewPageIndex.Text) - 1; } } else { newPageIndex = e.NewPageIndex; } newPageIndex = newPageIndex < 0 ? 0 : newPageIndex; newPageIndex = newPageIndex >= theGrid.PageCount ? theGrid.PageCount - 1 : newPageIndex; theGrid.PageIndex = newPageIndex; BindDate(); } private void BindDate() { SqlDataReader hh = SqlHelps.SqlHelper.Query("selecttitleID,titlename,classname,titletime from titlesTable where moudnumber=3"); if (null != hh) { GridView1.DataSource = hh; GridView1.DataBind(); } }
Repeater(Datalist) ( ) : (1).在这里写一个分页类PageNums:
using System; using System.Data; using System.Web; using System.Web.UI.WebControls; public class PageNums { public static string GetPageNum(DataSet ds, Repeater datalistname, int pagesize) { PagedDataSource objPds = new PagedDataSource(); objPds.DataSource = ds.Tables[0].DefaultView; objPds.AllowPaging = true; int total = ds.Tables[0].Rows.Count; objPds.PageSize = pagesize; int page; if (HttpContext.Current.Request.QueryString["page"] != null) page Convert.ToInt32(HttpContext.Current.Request.QueryString["page"]); else page = 1; objPds.CurrentPageIndex = page - 1; datalistname.DataSource = objPds; datalistname.DataBind(); int allpage = 0; int next = 0; int endcount = 0; if (pagesize != 0) { allpage = (total / pagesize); allpage = ((total % pagesize) != 0 ? allpage + 1 : allpage); allpage = (allpage == 0 ? 1 : allpage); } next = page + 1; pre = page - 1; startcount = (page + 5) > allpage ? allpage - 9 : page - 4; endcount = page < 5 ? 10 : page + 5; if (startcount < 1) { startcount = 1; } if (allpage < endcount) { endcount = allpage; } pagestr = "
" + "共" + allpage + "页&;nbsp;&;nbsp;&;nbsp;&;nbsp;"; pagestr += page > 1 "
首 页 &;nbsp;&;nbsp;
上一页" : if (page < 1) { page = 1; } int pre = 0; int startcount = 0; s
tring pagestr = ""; =
"
首页" + "&;nbsp;&;nbsp;" + "
上一页"; for (int i = startcount; i <= endcount; i++) { pagestr += page == i ? "&;nbsp;&;nbsp;" + "
" + i + "": "&;nbsp;&;nbsp;
" + i + ""; } pagestr += page != allpage ? "&;nbsp;&;nbsp;
下 一 页 &;nbsp;&;nbsp;
末页" : "&;nbsp;&;nbsp;" + "
下一页" + "&;nbsp;&;nbsp;" + "
末页"; return pagestr; } 在这里这是给Repeater实现分页 如果要是给datalist分页的话, datalist分页的话 在这里这是给 Repeater实现分页, 如果要是给 datalist 分页的话 , 就直接将最初的忽地参数中的 Repeater 实现分页, Repeater变成datalist即可。 Repeater变成datalist即可。 变成datalist即可
(2)在需要分页的控件最底部需要添加:
-
相关文章
-
最新文件