datalist怎么实现分页?
我找资料写了一个,当点击下一页的时候,错误信息说:试图除以零...
我找资料写了一个,当点击下一页的时候,错误信息说:试图除以零...
解决方案 »
- wcf超时,求高人解决
- Linq错误:无法删除尚未附加的实体
- 求助关于C#动态生成ImageList的问题
- C#新手,不知道选Vs2005和VS2008哪个好,请帮忙
- C#获取摄像头每一帧在内存中地址
- 【没有注册类别】32位系统运行正常的系统,在64位系统上运行不正常
- c#的一个类型转换的问题[给分哦]
- 如何让项目能兼容多种数据库,而不管我写的sql是哪种?即如何写一个sql parse?up有分!
- 在Tree view中怎样高亮 单击的结点(急!!在线等啊!!!!)
- C#判断十几个文本框的输入哪些有数据
- C#中关于两个form共用代码的问题
- C#中NumericUpDown控件的Value属性问题?
本群技术交流,疑难解答。大家 一起努力...
.NET开发群号: 38850938
你一个很好的分面控件 AspNetPager http://www.webdiyer.com 上有下载地址,和使用方法
参考:
http://www.cnblogs.com/wangzhq/archive/2007/01/08/615139.aspx
http://www.cnblogs.com/benon/articles/1114606.html
视频1:DataList显示/编辑数据
http://tech.163.com/06/1009/11/2T07K2M80009215D.html
视频2:DataLis分页/镶套
http://tech.163.com/06/1009/11/2T07LHTB0009215D.html
private void Page_Load(object sender, System.EventArgs e)
{
//页面初试化时进行数据绑定
if(!IsPostBack)
DataListDataBind();
}
private void DataListDataBind()
{
//定义数据连接对象,其中数据库连接字符串是在Web.Config文件中定义的
SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionSqlServer"].ToString());
//创建数据适配器对象
SqlDataAdapter da = new SqlDataAdapter("select EmployeeID,LastName,FirstName,Title,TitleOfCourtesy from Employees",conn);
//创建DataSet对象
DataSet ds = new DataSet();
try
{
//填充数据集
da.Fill(ds,"testTable");
//创建分页类
PagedDataSource objPage = new PagedDataSource();
//设置数据源
objPage.DataSource = ds.Tables["testTable"].DefaultView;
//允许分页
objPage.AllowPaging = true;
//设置每页显示的项数
objPage.PageSize = 5;
//定义变量用来保存当前页索引
int CurPage;
//判断是否具有页面跳转的请求
if (Request.QueryString["Page"] != null)
CurPage=Convert.ToInt32(Request.QueryString["Page"]);
else
CurPage=1;
//设置当前页的索引
objPage.CurrentPageIndex = CurPage-1;
//显示状态信息
lblCurPage.Text = "当前页:第" + CurPage.ToString()+"页";
//如果当前页面不是首页
if (!objPage.IsFirstPage)
//定义"上一页"超级链接的URL为:当前执行页面的虚拟路径,并传递下一页面的索引值
lnkPrev.NavigateUrl=Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage-1);
//如果当前页面不是最后一页
if (!objPage.IsLastPage)
//定义"下一页"超级链接的URL为:当前执行页面的虚拟路径,并传递下一页面的索引值
lnkNext.NavigateUrl=Request.CurrentExecutionFilePath+ "?Page=" + Convert.ToString(CurPage+1); //进行数据绑定
dlPager.DataSource = objPage;
dlPager.DataBind();
}
catch(Exception error)
{
//输出异常信息
Response.Write(error.ToString());
}
}
思路大体如下:DataList1.DataSource=GetPageRecord(int pageIndex,int PageSize,string condition,string orderConditon);DataList1.DataBind();下面加一个Lable用于显示分页的页码
根据上面的查询条件,得到记录总数然后用RecrodCount,pageIndex,pageSize,得到页码字符串,付给lable.Text,
如:
lble1.Text=GetPageStr(recordCount,pageIndex,pageSize,查询条件,跳转的页面);这样GetPageRecord这个方法,用于查询当前页的数据,
GetPageStr这个方法,用于得到某一页的显示字符串
int CurrentPage;
int PageSize;
string connection = "server=.;database=YaxinBookShop;uid=sa;pwd=123";
(DataListBind()是绑定DataList一个方法)
private void PageBind()
{
// 在此处放置用户代码以初始化页面
PageSize = 7;
//string sql = "select * from Message order by CreateTime desc ";
//计算总共有多少记录
int RecordCount = CalculateRecord();
//计算总共有多少页
//取整
int PageCount = RecordCount / PageSize;
if (RecordCount % PageSize > 0)
PageCount = PageCount + 1;
lblPageCount.Text = PageCount.ToString();
lblRecordCount.Text = RecordCount.ToString();
ViewState["PageCount"] = PageCount;
CurrentPage = 0;
ViewState["PageIndex"] = 0;
//绑定
ListBind();
DataListBind();
}
public int CalculateRecord()
{
int intCount;
string strCount = "select count(*) as co from Message";
//string connection = "server=.;database=YaxinBookShop;uid=sa;pwd=123";
SqlConnection Con = new SqlConnection(connection);
SqlCommand addCommand = new SqlCommand(strCount, Con);
addCommand.Connection.Open();
SqlDataReader dr;
dr=addCommand.ExecuteReader();
if(dr.Read())
{
intCount = Int32.Parse(dr["co"].ToString());
}
else
{
intCount = 0;
}
dr.Close();
return intCount;
}
ICollection CreateSource()
{ int StartIndex;
//设定导入的起终地址
StartIndex = CurrentPage*PageSize;
string strSel = "select * from Message";
//SqlConnection Con=new SqlConnection(data.constr);
SqlConnection Con = new SqlConnection(connection);
DataSet ds = new DataSet();
SqlDataAdapter MyAdapter = new SqlDataAdapter(strSel,Con);
MyAdapter.Fill(ds,StartIndex,PageSize,"Message");
return ds.Tables["Message"].DefaultView;
}
public void ListBind()
{
DataList1 .DataSource = CreateSource();
DataList1.DataBind();
lbnNextPage.Enabled = true;
lbnPrevPage.Enabled = true; //计算总共有多少记录
int RecordCount = CalculateRecord();
//计算总共有多少页
//取整
int PageCount = RecordCount / PageSize+1;
if(PageCount==0)
{
lblCurrentPage.Text = "0";
lbnNextPage.Enabled = false;
lbnPrevPage.Enabled = false;
}
else
{
if(CurrentPage==(PageCount-1)) lbnNextPage.Enabled = false;
if(CurrentPage==0) lbnPrevPage.Enabled = false;
lblCurrentPage.Text = (CurrentPage+1).ToString();
}
DataListBind();
}
protected void Page_OnClick(object sender, CommandEventArgs e)
{
CurrentPage = (int)ViewState["PageIndex"];
int PageCount = (int)ViewState["PageCount"];
string cmd = e.CommandName;
//判断cmd,以判定翻页方向
switch(cmd)
{
case "next":
if(CurrentPage<(PageCount-1)) CurrentPage++;
break;
case "prev":
if(CurrentPage>0) CurrentPage--;
break;
case "first":
CurrentPage=0;
break;
case "last":
CurrentPage=PageCount-1;
break;
} ViewState["PageIndex"] = CurrentPage; ListBind();
DataListBind();}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Bind();
}
} public void Bind()
{
int curpage = Convert.ToInt32(labNowPage.Text);
PagedDataSource ps = new PagedDataSource();
OleDbConnection myCon = DB.Creatconnection();
string ComStr = "select * from Imageinfo order by ImageDatetime DESC";
OleDbDataAdapter myDA = new OleDbDataAdapter(ComStr, myCon);
DataSet myDS = new DataSet();
myDA.Fill(myDS, "Imageinfo");
ps.DataSource = myDS.Tables["Imageinfo"].DefaultView;
ps.AllowPaging = true; //是否可以分页
ps.PageSize = 5; //显示的数量
ps.CurrentPageIndex = curpage - 1; //取得当前页的页码
lnkbtn_Prve.Enabled = true;
lnkbtn_Top.Enabled = true;
lnkbtn_Next.Enabled = true;
lnkbtn_Last.Enabled = true;
if (curpage == 1)
{
lnkbtn_Top.Enabled = false;//不显示第一页按钮
lnkbtn_Prve.Enabled = false;//不显示上一页按钮
}
if (curpage == ps.PageCount)
{
lnkbtn_Next.Enabled = false;//不显示下一页
lnkbtn_Last.Enabled = false;//不显示最后一页 }
this.labCount.Text = Convert.ToString(ps.PageCount);
this.DataList1.DataSource = ps;
this.DataList1.DataKeyField = "Imagefilename";
this.DataList1.DataBind();
}
//首页
protected void lnkbtnTop_Click(object sender, EventArgs e)
{
this.labNowPage.Text = "1";
this.Bind();
}
//上一页
protected void lnkbtnPrve_Click(object sender, EventArgs e)
{
this.labNowPage.Text = Convert.ToString(Convert.ToInt32(this.labNowPage.Text) - 1);
this.Bind();
}
//下一页
protected void lnkbtnNext_Click(object sender, EventArgs e)
{
this.labNowPage.Text = Convert.ToString(Convert.ToInt32(this.labNowPage.Text) + 1);
this.Bind();
}
//尾页
protected void lnkbtnLast_Click(object sender, EventArgs e)
{
this.labNowPage.Text = this.labCount.Text;
this.Bind();
}