DataList分页的话需要后台代码
用GridView自带分页
翻页的话,有几种方法,最简单的是用自带的翻页功能,在就是还可以你自己写一些翻页方法,在页面调用给datalist,我用的是一个叫aspnetpager的翻页控件,你可以在网上搜搜,用法跟其他控件一样,直接拖到页面上就行了,然后就是后台代码绑定,给你一个我以前绑定的方法看看:
private void DataBinds()
{
PagedDataSource pds = new PagedDataSource();
pds.DataSource = BookManager.GetNewBooks();//写你自己的方法
pds.AllowPaging = true;
pds.CurrentPageIndex = this.AspNetPager1.CurrentPageIndex - 1;
this.AspNetPager1.RecordCount = 20;//数据总数,我这里直接写20,应该是你数据count()值
pds.PageSize = this.AspNetPager1.PageSize;
this.datalist.DataSource = pds;
this.datalist.DataBind();
}
public void databinds(string id,string bigcode,string smallcode,string count)
{
bool pageshow=true;
DataSet ds_news=Q.Qry_type_news("",bigcode,smallcode,"");
if (ds_news.Tables[0].Rows.Count>0)
{
PagedDataSource objPage = new PagedDataSource();
//设置数据源
objPage.DataSource = ds_news.Tables[0].DefaultView;
//允许分页
objPage.AllowPaging = true;
//设置每页显示的项数
objPage.PageSize = 14;
//定义变量用来保存当前页索引
int CurPage=1;
//判断是否具有页面跳转的请求
if(!pageshow)
CurPage = 1;
else if (Request.QueryString["Page"] != null)
CurPage=Convert.ToInt32(Request.QueryString["Page"]);
//设置当前页的索引
int countnumber = ds_news.Tables[0].Rows.Count;
double db = countnumber/objPage.PageSize;
int n = int.Parse(db.ToString());
if(countnumber!=objPage.PageSize*n)
{
n+=1;
}
if (n==0) n = 1;
objPage.CurrentPageIndex = CurPage-1;
LblCurrentIndex.Text = "第 " + (objPage.CurrentPageIndex + 1).ToString() + " 页";
LblPageCount.Text = "共 " + objPage.PageCount.ToString() + " 页";
LblRecordCount.Text = "总共 " + ds_news.Tables[0].Rows.Count.ToString() + " 条";
if(CurPage>=objPage.PageCount)
{
btnNext.Enabled=false;
}
else if (CurPage<=1)
{
btnPrev.Enabled=false;
}
btnFirst.NavigateUrl=Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(1)+Session["big"].ToString();
btnNext.NavigateUrl=Request.CurrentExecutionFilePath +"?Page="+Convert.ToString(CurPage+1)+Session["big"].ToString();
btnPrev.NavigateUrl=Request.CurrentExecutionFilePath +"?Page="+Convert.ToString(CurPage-1)+Session["big"].ToString();
btnLast.NavigateUrl=Request.CurrentExecutionFilePath +"?Page="+Convert.ToString(objPage.PageCount.ToString())+Session["big"].ToString();
this.DataList1.DataSource=objPage;
this.DataList1.DataBind();
for(int i=1;i<=int.Parse(objPage.PageCount.ToString());i++)
{
string s="";
s=s+Convert.ToString(i);
this.dropnum.Items.Add(s);
}
if (ds_news.Tables[0].Rows.Count == 0)
{
btnFirst.Visible = false;
btnPrev.Visible = false;
btnNext.Visible = false;
btnLast.Visible = false;
LblCurrentIndex.Visible = false;
LblPageCount.Visible = false;
LblRecordCount.Visible = false;
}
else if (objPage.PageCount == 1)
{
btnFirst.Visible = false;
btnPrev.Visible = false;
btnNext.Visible = false;
btnLast.Visible = false;
}
}
else
{
ed.Visible = true;
btnPrev.Visible=false;
btnNext.Visible=false;
btnFirst.Visible=false;
btnLast.Visible=false;
ed.Text = "无新闻发布";
this.dropnum.Visible=false;
this.lblp.Visible=false;
this.lblz.Visible=false;
}
}
#endregion
public string SplieStr(object str,int n)
{
string s = str.ToString();
if(s.Length > n)
{
s = s.Substring(0,n)+"…";
}
return s;
}
++++++++++++++++++++
哈哈 还 dataview
是Gridview 不会就别装懂
搞笑