DataGrid 分页必须考虑的几个问题
2004-11-02 20:47
363 查看
Asp.net中的DataGrid分页必须考虑以下几个问题
1。如果数据不多,则不分页,多则分页
if(DataGrid1.Items.Count < DataGrid1.PageSize+1)
DataGrid1.AllowPaging = false;
else
{
DataGrid1.AllowPaging = true;
}
DataGrid1.DataBind();
if(DataGrid1.Items.Count < DataGrid1.PageSize+1)
DataGrid1.AllowPaging = false;
else
{
DataGrid1.AllowPaging = true;
}
DataGrid1.DataBind();
但是我现在还不知道为什么要datebind 2次。如果不这样处理的话,数据不够的话,也会显示页吗1。
2。后一页最后一条记录删除,需要将他的CurrentPageIndex减一。
if (DataGrid1.Items.Count==1)
if(DataGrid1.CurrentPageIndex !=0)
DataGrid1.CurrentPageIndex-=1;
3。如果页面上提供了查询功能时,一定要在DateBind之前把CurrentPageIndex 设为0,否则很有可能出错,因为有可能查询前DataGrid的CurrentPageIndex >查询后DataGrid的PageCount。
4。分页时,先显示第一页,在判断能不能分页
DataGrid1.CurrentPageIndex = 0;
DataGrid1.DataBind();
if (e.NewPageIndex < DataGrid1.PageCount )
{
DataGrid1.CurrentPageIndex = e.NewPageIndex;
DataGrid1.DataBind();
}
建了一个类,专门处理datagrid的显示,分页等 public class XuDataGrid
{
public XuDataGrid()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public static void BindDeleteData(string StrSql,DataGrid g)
{
if(g.AllowPaging)
{
if (g.Items.Count==1)
if(g.CurrentPageIndex !=0)
g.CurrentPageIndex-=1;
if(!pBindDataDelete(StrSql,g))
BindDataInitial(StrSql,g);
}
else
BindData(StrSql,g);
}
private static bool pBindDataDelete(string StrSql,DataGrid g)
{
try
{
g.DataSource = ConnectionManager.GetDataView(StrSql);
if(g.EditItemIndex !=-1)
g.EditItemIndex =-1;
if(g.Items.Count < g.PageSize+1 )
g.AllowPaging = false;
else
g.AllowPaging = true;
g.DataBind();
if(g.Items.Count < g.PageSize+1 )
g.AllowPaging = false;
else
g.AllowPaging = true;
g.DataBind();
}
catch
{
return false;
}
return true;
}
private static bool pBindDataPage(string StrSql,DataGrid g,int IntNewPage)
{
try
{
g.DataSource = ConnectionManager.GetDataView(StrSql);
if(g.EditItemIndex !=-1)
g.EditItemIndex =-1;
g.CurrentPageIndex =IntNewPage;
g.DataBind();
}
catch
{
return false;
}
return true;
}
private static bool pBindDataEdit(string StrSql,DataGrid g,int IntEditIndex)
{
try
{
g.DataSource = ConnectionManager.GetDataView(StrSql);
g.EditItemIndex =IntEditIndex;
g.DataBind();
}
catch
{
return false;
}
return true;
}
public static void BindData(string StrSql,DataGrid g)
{
g.DataSource = ConnectionManager.GetDataView(StrSql);
g.EditItemIndex =-1;
g.DataBind();
}
//允许分页 --得到所有数据,初始化状态,同时要2次邦定
public static void BindDataAllowPaging(string StrSql,DataGrid g)
{
//BindDataInitial(StrSql,g);
g.DataSource = ConnectionManager.GetDataView(StrSql);
g.CurrentPageIndex =0;
g.EditItemIndex =-1;
if(g.Items.Count < g.PageSize+1 )
g.AllowPaging = false;
else
g.AllowPaging = true;
g.DataBind();
if(g.Items.Count < g.PageSize+1 )
g.AllowPaging = false;
else
g.AllowPaging = true;
g.DataBind();
}
public static void BindEditData(string StrSql,int IntEditIndex,DataGrid g)
{
if(g.AllowPaging)
{
if(!pBindDataEdit(StrSql,g,IntEditIndex))
BindDataInitial(StrSql,g);
//iBindEditData(StrSql,IntEditIndex,g.CurrentPageIndex,g);
}
else
iBindEditData(StrSql,IntEditIndex,g);
}
//没有分页 --编辑数据
private static void iBindEditData(string StrSql,int IntEditIndex,DataGrid g)
{
g.DataSource = ConnectionManager.GetDataView(StrSql);
if(IntEditIndex < g.Items.Count)
g.EditItemIndex =IntEditIndex;
g.DataBind();
}
public static void BindPageData(string StrSql,int IntPageIndex,DataGrid g)
{
if(!pBindDataPage(StrSql,g,IntPageIndex))
BindDataInitial(StrSql,g);
}
private static void BindDataInitial(string StrSql,DataGrid g)
{
g.DataSource = ConnectionManager.GetDataView(StrSql);
g.CurrentPageIndex =0;
g.EditItemIndex =-1;
if(g.Items.Count < g.PageSize+1 )
g.AllowPaging = false;
else
g.AllowPaging = true;
g.DataBind();
if(g.Items.Count < g.PageSize+1 )
g.AllowPaging = false;
else
g.AllowPaging = true;
g.DataBind();
}
//允许分页 --得到更新后数据数据
public static void BindUpdateData(string StrSql,DataGrid g)
{
BindEditData(StrSql,-1,g);
}
public static void BindCancelData(string StrSql,DataGrid g)
{
BindEditData(StrSql,-1,g);
}
}
1。如果数据不多,则不分页,多则分页
if(DataGrid1.Items.Count < DataGrid1.PageSize+1)
DataGrid1.AllowPaging = false;
else
{
DataGrid1.AllowPaging = true;
}
DataGrid1.DataBind();
if(DataGrid1.Items.Count < DataGrid1.PageSize+1)
DataGrid1.AllowPaging = false;
else
{
DataGrid1.AllowPaging = true;
}
DataGrid1.DataBind();
但是我现在还不知道为什么要datebind 2次。如果不这样处理的话,数据不够的话,也会显示页吗1。
2。后一页最后一条记录删除,需要将他的CurrentPageIndex减一。
if (DataGrid1.Items.Count==1)
if(DataGrid1.CurrentPageIndex !=0)
DataGrid1.CurrentPageIndex-=1;
3。如果页面上提供了查询功能时,一定要在DateBind之前把CurrentPageIndex 设为0,否则很有可能出错,因为有可能查询前DataGrid的CurrentPageIndex >查询后DataGrid的PageCount。
4。分页时,先显示第一页,在判断能不能分页
DataGrid1.CurrentPageIndex = 0;
DataGrid1.DataBind();
if (e.NewPageIndex < DataGrid1.PageCount )
{
DataGrid1.CurrentPageIndex = e.NewPageIndex;
DataGrid1.DataBind();
}
建了一个类,专门处理datagrid的显示,分页等 public class XuDataGrid
{
public XuDataGrid()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public static void BindDeleteData(string StrSql,DataGrid g)
{
if(g.AllowPaging)
{
if (g.Items.Count==1)
if(g.CurrentPageIndex !=0)
g.CurrentPageIndex-=1;
if(!pBindDataDelete(StrSql,g))
BindDataInitial(StrSql,g);
}
else
BindData(StrSql,g);
}
private static bool pBindDataDelete(string StrSql,DataGrid g)
{
try
{
g.DataSource = ConnectionManager.GetDataView(StrSql);
if(g.EditItemIndex !=-1)
g.EditItemIndex =-1;
if(g.Items.Count < g.PageSize+1 )
g.AllowPaging = false;
else
g.AllowPaging = true;
g.DataBind();
if(g.Items.Count < g.PageSize+1 )
g.AllowPaging = false;
else
g.AllowPaging = true;
g.DataBind();
}
catch
{
return false;
}
return true;
}
private static bool pBindDataPage(string StrSql,DataGrid g,int IntNewPage)
{
try
{
g.DataSource = ConnectionManager.GetDataView(StrSql);
if(g.EditItemIndex !=-1)
g.EditItemIndex =-1;
g.CurrentPageIndex =IntNewPage;
g.DataBind();
}
catch
{
return false;
}
return true;
}
private static bool pBindDataEdit(string StrSql,DataGrid g,int IntEditIndex)
{
try
{
g.DataSource = ConnectionManager.GetDataView(StrSql);
g.EditItemIndex =IntEditIndex;
g.DataBind();
}
catch
{
return false;
}
return true;
}
public static void BindData(string StrSql,DataGrid g)
{
g.DataSource = ConnectionManager.GetDataView(StrSql);
g.EditItemIndex =-1;
g.DataBind();
}
//允许分页 --得到所有数据,初始化状态,同时要2次邦定
public static void BindDataAllowPaging(string StrSql,DataGrid g)
{
//BindDataInitial(StrSql,g);
g.DataSource = ConnectionManager.GetDataView(StrSql);
g.CurrentPageIndex =0;
g.EditItemIndex =-1;
if(g.Items.Count < g.PageSize+1 )
g.AllowPaging = false;
else
g.AllowPaging = true;
g.DataBind();
if(g.Items.Count < g.PageSize+1 )
g.AllowPaging = false;
else
g.AllowPaging = true;
g.DataBind();
}
public static void BindEditData(string StrSql,int IntEditIndex,DataGrid g)
{
if(g.AllowPaging)
{
if(!pBindDataEdit(StrSql,g,IntEditIndex))
BindDataInitial(StrSql,g);
//iBindEditData(StrSql,IntEditIndex,g.CurrentPageIndex,g);
}
else
iBindEditData(StrSql,IntEditIndex,g);
}
//没有分页 --编辑数据
private static void iBindEditData(string StrSql,int IntEditIndex,DataGrid g)
{
g.DataSource = ConnectionManager.GetDataView(StrSql);
if(IntEditIndex < g.Items.Count)
g.EditItemIndex =IntEditIndex;
g.DataBind();
}
public static void BindPageData(string StrSql,int IntPageIndex,DataGrid g)
{
if(!pBindDataPage(StrSql,g,IntPageIndex))
BindDataInitial(StrSql,g);
}
private static void BindDataInitial(string StrSql,DataGrid g)
{
g.DataSource = ConnectionManager.GetDataView(StrSql);
g.CurrentPageIndex =0;
g.EditItemIndex =-1;
if(g.Items.Count < g.PageSize+1 )
g.AllowPaging = false;
else
g.AllowPaging = true;
g.DataBind();
if(g.Items.Count < g.PageSize+1 )
g.AllowPaging = false;
else
g.AllowPaging = true;
g.DataBind();
}
//允许分页 --得到更新后数据数据
public static void BindUpdateData(string StrSql,DataGrid g)
{
BindEditData(StrSql,-1,g);
}
public static void BindCancelData(string StrSql,DataGrid g)
{
BindEditData(StrSql,-1,g);
}
}
相关文章推荐
- 大家有没有考虑过这个问题:datagrid分页,viewstate到底起什么作用?
- 在考虑如何做微信营销之前,你必须思考下面几个问题。
- WebSocket :用WebSocket实现推送你必须考虑的几个问题
- 布线规划要点-开始设计前必须考虑的几个问题
- O2O风行的时代汽车电商创业成功必须考虑清楚的几个问题
- web.config中的<globalization >标签在将几个不同服务器上的网闸内容以统一面貌集成在自己网站上时的要考虑的问题
- 大学生择业必须考虑的最关键问题
- 聊聊软件与吃饭(二)- 谈谈甲方选择软件供应商必须要分析的几个问题
- 关于DataGrid自动分页的不能显示的问题
- 搭建项目框架时需要考虑的几个问题
- 大型C++项目必须注意的几个小问题
- 一个优美的架构需要考虑的几个问题
- 如果网页内容需要支持多语言,你会怎么做?在设计和开发多语言网站时,有哪些问题你必须要考虑?
- 作为大型网站架构必须考虑的十大问题
- 总结目前用Jquery-Easyui做后台,遇到datagrid分页及数据刷新问题
- datagrid分页问题(前后跳页)《控件版继承usercontrol》
- mysql索引必须了解的几个重要问题
- Web应用接口应该考虑的几个问题
- 在线考试系统设计时必须考虑的问题之三----------考试题库问题
- 如果网页内容需要支持多语言,你会怎么做?在设计和开发多语言网站时,有哪些问题你必须要考虑?