详解ASP.NET数据绑定操作中Repeater控件的用法(3)

<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> <style type="text/css"> .pageBar { margin-top: 10px; } .pageBar a { color: #333; font-size: 12px; margin-right: 10px; padding: 4px; border: 1px solid #ccc; text-decoration: none; } </style> </head> <body> <form runat="server"> <div> <asp:Repeater runat="server" > <HeaderTemplate> <table cellpadding="0" cellspacing="0"> <tr> <th>ID</th> <th>内容</th> </tr> </HeaderTemplate> <ItemTemplate> <tr> <td><asp:Label runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"id") %>' ></asp:Label></td> <td><%# DataBinder.Eval(Container.DataItem,"name") %></td> </tr> </ItemTemplate> <FooterTemplate> </table> </FooterTemplate> </asp:Repeater> </div> <div> <asp:Literal runat="server"></asp:Literal> </div> </form> </body> </html>

后台代码:Repeater控件的数据源是PagedDataSource对象,在页面加载时为该对象动态指定了分页的属性,并使用Literal标签来动态指定每个标签跳转页的链接。

using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; using System.Text; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace WebApplication4 { public partial class PageDemo : System.Web.UI.Page { private string id = ""; protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { //设置当前页的索引 int pageIndex = 1; try { //获取当前索要跳转页的索引号 pageIndex = Convert.ToInt32(Request.QueryString["Page"]); //如果是第0页将会跳转入第1页 if (pageIndex <= 0) { pageIndex = 1; } } catch { pageIndex = 1; } DataTable dt = this.GetDataTable(); //获取绑定的数据表 PagedDataSource pds = new PagedDataSource(); //创建分页数据源对象 pds.DataSource = dt.DefaultView; //为数据源对象设置数据源 pds.AllowPaging = true; //对象允许分页 pds.PageSize = 2; //设置对象每页显示的大小 pds.CurrentPageIndex = pageIndex - 1; //设置数据源的当前页 //向Repeater控件上绑定分页数据源控件 this.Repeater1.DataSource = pds; this.Repeater1.DataBind(); //使用Literal标签来动态指定每个标签跳转页的链接 ltlPageBar.Text = this.GetPageBar(pds); } } /// <summary> /// 获取每个标签上的跳转页的链接地址 /// </summary> /// <param>分页数据源对象</param> /// <returns>分页操作按钮的html文本</returns> private string GetPageBar(PagedDataSource pds) { string pageBar = string.Empty; //声明页面标签文本 int currentPageIndex = pds.CurrentPageIndex + 1; //获取当前页索引 //判断首页的链接页面 if (currentPageIndex == 1) //如果该页为第一页,则证明它为首页 { pageBar += "<a href=https://www.jb51.net/article/\"javascript:void(0)\">首页</a>"; } else { //如果不是首页,首页链接的地址将会为1 pageBar += "<a href=https://www.jb51.net/article/\"" + Request.CurrentExecutionFilePath + "?Page=1\">首页</a>"; } //判断上一页链接的地址 if ((currentPageIndex - 1) < 1) //如果上一页小于1则链接到第一页 { pageBar += "<a href=https://www.jb51.net/article/\"javascript:void(0)\">上一页</a>"; } else { //如果上一页地址不是1将会链接到上一页 pageBar += "<a href=https://www.jb51.net/article/\"" + Request.CurrentExecutionFilePath + "?Page=" + (currentPageIndex - 1) + "https://www.jb51.net/article/\">上一页</a>"; } //指定下一页的链接地址 if ((currentPageIndex + 1) > pds.PageCount) { //如果下一页的地址大于总页数,将会连接到首页 pageBar += "<a href=https://www.jb51.net/article/\"javascript:void(0)\">下一页</a>"; } else { //否则的话链接到下一页 pageBar += "<a href=https://www.jb51.net/article/\"" + Request.CurrentExecutionFilePath + "?Page=" + (currentPageIndex + 1) + "https://www.jb51.net/article/\">下一页</a>"; } //指定末页的链接地址 if (currentPageIndex == pds.PageCount) { pageBar += "<a href=https://www.jb51.net/article/\"javascript:void(0)\">末页</a>"; } else { pageBar += "<a href=https://www.jb51.net/article/\"" + Request.CurrentExecutionFilePath + "?Page=" + pds.PageCount + "https://www.jb51.net/article/\">末页</a>"; } return pageBar; //返回html文本 } /// <summary> /// 获取数据源,重新链接数据 /// </summary> /// <returns>DataTable,数据源</returns> private DataTable GetDataTable() { DataTable dt = new DataTable(); //创建数据库表 using (SqlConnection con = new SqlConnection("server=.;DataBase=MyBlog;uid=sa;pwd=1;")) { con.Open(); //打开数据库链接 SqlCommand sqlCom = new SqlCommand(); //声明并创建数据库命令集 StringBuilder sqlStr = new StringBuilder(); //声明sql语句 sqlStr.Append("select * from match"); //获取sql语句 sqlCom.CommandText = sqlStr.ToString(); //为sqlcommand对象指定sql语句 sqlCom.Connection = con; //为sqlcommand对象指定链接对象 SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCom); //声明数据库适配器 SqlCommandBuilder sqlBuilder = new SqlCommandBuilder(sqlDa); sqlDa.Fill(dt); //填充表 } return dt; } } }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wjypgx.html