惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

B
Blog RSS Feed
J
Java Code Geeks
H
Help Net Security
Google DeepMind News
Google DeepMind News
博客园 - 司徒正美
Microsoft Security Blog
Microsoft Security Blog
宝玉的分享
宝玉的分享
Stack Overflow Blog
Stack Overflow Blog
D
DataBreaches.Net
The GitHub Blog
The GitHub Blog
S
SegmentFault 最新的问题
U
Unit 42
博客园 - 三生石上(FineUI控件)
Last Week in AI
Last Week in AI
M
MIT News - Artificial intelligence
WordPress大学
WordPress大学
小众软件
小众软件
博客园 - 叶小钗
D
Docker
量子位
P
Proofpoint News Feed
博客园_首页
T
Tailwind CSS Blog
F
Fortinet All Blogs

博客园 - gengen

asp.net程序中最常用的三十三种编程代码 js+xml dataset读取xml 下拉 flash实现 js+xml 简单上传 Datalist分页 datagrid 绑定与简单分页 asp.net 常用文件上传下载 转的 批量删除 移动控件 可拖放控件的使用 国家公务员考完了 RegularExpressionValidator javascript中模拟hashtable对数组进行快速查找 网页中提交按钮倒记时的实现 原作:mlg js技巧 WEB标准开发中的一些基本用法(连载中...)
DataList分页(与Repeater控件的例子相似,都以pubs数据库中的a...
gengen · 2006-12-27 · via 博客园 - gengen

DataList分页(与Repeater控件的例子相似,都以pubs数据库中的authors为例)

2006年12月27日 星期三 10:10

DataList控件和Repeater控件的分页显示方法很相似,都是借助PagedDataSource类来实现,该类封装了DataGrid控件的分页属性.

1首先在页前代码:

 <ItemTemplate>
     <%#DataBinder.Eval(Container.DataItem,"address")%>
     <%#DataBinder.Eval(Container.DataItem,"city")%>
     <%#DataBinder.Eval(Container.DataItem,"zip")%>
    </ItemTemplate>

2、然后在后台代码
  private void Page_Load(object sender, System.EventArgs e)
  {
   // 在此处放置用户代码以初始化页面
   if(!IsPostBack)
   {
    DataListDataBind();}
  }

其次定义一个数据绑定的方法

private void DataListDataBind()
  {
   SqlConnection conn=new SqlConnection(ConfigurationSettings.AppSettings["con"].ToString());
   string sql="select address,city,zip from authors";
   SqlDataAdapter da=new SqlDataAdapter(sql,conn);
   DataSet ds=new DataSet();
   try
   {
    da.Fill(ds,"authors");
    PagedDataSource objPage1=new PagedDataSource();
    objPage1.DataSource=ds.Tables["authors"].DefaultView;
    objPage1.AllowPaging=true;
    objPage1.PageSize=5;
    int curPage;
    if(Request.QueryString["Page"]!=null)
     curPage=Convert.ToInt32(Request.QueryString["Page"]);
    else
     curPage=1;
    objPage1.CurrentPageIndex=curPage-1;
    this.Label2.Text="当前页:第"+curPage.ToString()+"页";
    if(!objPage1.IsFirstPage)
    {
     this.HyperLink3.NavigateUrl=Request.CurrentExecutionFilePath+"?Page="+Convert.ToString(curPage-1);
    }
    if(!objPage1.IsLastPage)
    {
     this.HyperLink4.NavigateUrl=Request.CurrentExecutionFilePath+"?Page="+Convert.ToString(curPage+1);
    }
    this.DataList1.DataSource=objPage1;
    this.DataList1.DataBind();
   }
   catch(Exception error)
   {
    Response.Write(error.ToString());
   }