ASP.NET2.0 GridView控件两大常见问题解决方案:自定义数据分页,类似DataGrid的VirtualItemCount功能
来源:互联网 发布:淘宝摄影教程 编辑:程序博客网 时间:2024/06/05 21:03
GridView是asp.net2.0新推出的控件,用来取代DataGrid。对于很多简单操作,的确方便了不少,例如不写一句代码结合sqldatasource实现增删改、翻页等操作。
但在很多细粒度的控制方面,反而不如DataGrid,所以大部分时间,笔者依然采用DataGrid,近日重写GridView控件,研究了一下如果通过GridView实现自定义数据分页,成功解决了这个问题。
GridView分页功能比DataGrid强大了不少,但没有类似dataGrid的VirtualItemCount属性,如何实现自定分页成了难题,翻阅若干资料,终于找到了ObjectDataSource这个对象辅助完成的解决办法,经过改造,成功实现了想要的功能。现把实现思路给大家讲一讲:
定义适配器类
internal class ObjectDataSourceAdapter
{
private object _dt;
private int _vic;
//this is the constructor that takes as parameters a cute little datatable
// with the right 10 records, an an integer vic = virtualitemcount
public ObjectDataSourceAdapter(object dt, int vic)
{
_dt = dt;
_vic = vic;
}
//this returns the datatable (10 records)
public object GetData()
{
return _dt;
}
//this returns the total number of records in the table (30.000)
public int VirtualItemCount()
{
return _vic;
}
//this also returns the datatable (10 records) but the ODS needs it for paging purposes
public object GetData(int startRow, int maxRows)
{
return _dt;
}
}
设置ObjectDataSource为数据源
ObjectDataSource ods = new ObjectDataSource();
ods.ID = "ods" ;
ods.EnablePaging = this.AllowPaging; //the paging of ODS depends on the paging of the GridView
ods.TypeName = "ObjectDataSourceAdapter"; //be sure to prefix the namespace of your application !!! e.g. MyApp.MyTableAdapter
ods.SelectMethod = "GetData";
ods.SelectCountMethod = "VirtualItemCount";
ods.StartRowIndexParameterName = "startRow";
ods.MaximumRowsParameterName = "maxRows";
ods.EnableViewState = false;
ods.ObjectCreating += new ObjectDataSourceObjectEventHandler(ods_ObjectCreating);
this.DataSource = ods;
this.DataBind();
委托方法
private void ods_ObjectCreating(object sender, ObjectDataSourceEventArgs e)
{
e.ObjectInstance = new ObjectDataSourceAdapter(dt, this.RecordCount);
}
这里的dt就是你的分页数据,支持datatable和datareader RecordCount就是VirtualItemCount
- ASP.NET2.0 GridView控件两大常见问题解决方案:自定义数据分页,类似DataGrid的VirtualItemCount功能
- ASP.NET2.0 GridView控件两大常见问题解决方案:始终显示页眉页脚
- asp.net2.0分页的实现(GridView)
- ASP.NET:DataGrid控件的分页功能
- ASP.NET:DataGrid控件的分页功能
- 自定义Gridview控件的分页功能
- asp.net2.0下一个标准GRIDVIEW功能的实现(不用datasource控件)
- asp.net2.0下一个标准GRIDVIEW功能的实现(不用datasource控件)
- ASP.NET2.0中用Gridview控件操作数据
- ASP.NET2.0中用Gridview控件操作数据
- ASP.NET2.0中用Gridview控件操作数据
- ASP.NET2.0中用Gridview控件操作数据
- ASP.NET2.0中用Gridview控件操作数据
- ASP.NET2.0中用Gridview控件操作数据
- ASP.NET2.0中用Gridview控件操作数据
- 一个带自定义分页,排序功能的DATAGRID控件
- 具有自定义分页和排序功能的DataGrid控件
- ASP.NET中利用DataGrid的自定义分页功能
- 正则表达式大全
- string函数总结(2)
- 在程序集中内嵌js资源
- ASP.NET 2.0移动开发样式使用
- JS的IE和Firefox兼容性汇编
- ASP.NET2.0 GridView控件两大常见问题解决方案:自定义数据分页,类似DataGrid的VirtualItemCount功能
- 正则表达式语法
- vc++ 如何使radio button ,checkbox初始为已选状态?
- VC编程中,如何使用Radio单选按钮?
- SAP中发消息报表程序
- 嵌入式Web 服务器Boa 的移植及其应用
- 一个计算机专业学生几年的编程经验汇总
- INTRDR
- 嵌入式Linux系统CGI程序设计技术