ObjectDataSource 数据源分页机制(转)

来源:互联网 发布:淘宝放单主持靠谱吗 编辑:程序博客网 时间:2024/05/21 00:55

简而言之:比起没有分页多了四样东西:

1.一个允许分页熟悉

2.一个获取数据源count绑定函数

34.select函数多了两个参数(一个是开始index,一个是最大index)

示例:

一、并为他们配置属性。

HTML
  <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" EnablePaging="True" TypeName="DataCmd"            SelectCountMethod="GetLimingchStudioCount" SelectMethod="GetProduct"             MaximumRowsParameterName="maximumRows"             StartRowIndexParameterName="startRowIndex">        </asp:ObjectDataSource>
codeBehind
using System;using System.Collections.Generic;using System.ComponentModel;using System.Configuration;using System.Data;using System.Data.Common;using System.Data.OleDb;using System.Web;/// <summary>///DataCmd 的摘要说明/// </summary>public class DataCmd{    public DataCmd()    {    }    string _connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\\sanguo.mdb;Persist Security Info=False";    public DataTable GetProduct(int startRowIndex, int maximumRows)    {        OleDbConnection con = new OleDbConnection(_connectionString);        string selectString = "SELECT * FROM [Sheet1]";               con.Open();           OleDbDataAdapter da = new OleDbDataAdapter(selectString, _connectionString);       // 建立一个 DataSet 对象。         DataSet ds = new DataSet();        using (da)         {             // 从 startRowIndex 参数所指定的数据行开始,提取 maximumRows 参数所指定的笔数,            // 然后将它们填入 DataSet 对象中的「章立民研究室」数据表。             da.Fill(ds, startRowIndex, maximumRows, "章立民研究室");           }          // 传回 DataTable 物件。        if (ds.Tables["章立民研究室"] != null)        {            return ds.Tables["章立民研究室"];        }         return null;    }    //通过HttpContext返回数据表的行数,便于ObjectDataSouce使用,对应ObjectDataSource1属性SelectCountMethod    public int GetLimingchStudioCount()    {        string preName=this.GetType().ToString();        HttpContext context = HttpContext.Current;        if (context.Cache[preName + "LimingchStudioCount"] == null)        {            int nRows = 0;            // 建立一个连接对象。            OleDbConnection con = new OleDbConnection(_connectionString);            // 建立一个数据命令对象。            OleDbCommand cmd = new OleDbCommand();            cmd.Connection = con;            cmd.CommandText = "SELECT Count(*) FROM [Sheet1]";            // 运行命令。            using (con)            {                con.Open();                nRows = (int)cmd.ExecuteScalar();            }            context.Cache[preName+"LimingchStudioCount"] = nRows;        }        return (int)context.Cache[preName+"LimingchStudioCount"];    }  }

 

 

看不懂没关系,再往下看:

再解释一遍:

这个控件的使用很简单,我们只需要配置几个属性就可以了。
  SelectMethod:指定用于获取分页数据的方法名。这个方法是一个自定义的.NET方法,你可以写在页面的CodeBehind代码中,将方法的名字给ObjectDataSource控件的SelectMethod即可。ObjectDataSource控件会通过委托的方式自动去执行你所指定的这个方法。
  TypeName:使用ObjectDataSource控件的类的全名称(包括名称空间和类名)。这个属性必须指定,ASP.NET会通过反射来加载相应的方法和对象。
  DataObjectTypeName:数据源对象的类型全名称。ObjectDataSource控件最大的亮点就在于它完全支持面向对象数据操作。在分层应用程序开发中,数据访问层的代码会将数据库中的表抽象为class对象,将数据库表中的字段抽象为class对象中的属性,DataObjectTypeName属性所指定的就是这个数据库类对象。
  EnablePaging:如果你想开启数据分页功能,就需要将这个属性的值设置为True。
  MaximumRowsParameterName:这个属性的值是一个参数名(只是一个参数名,而不是每页显示的数据条数),用于指示每页要显示数据的条数,ObjectDataSource控件根据委托在之前SelectMethod属性所指定的方法中传递该参数并执行其中的代码。注意,这个参数的名称必须与SelectMethod属性所指定的方法中的参数名称完全一样。
  StartRowIndexParameterName:这个属性也是一个参数名,用于指示每页起始记录的索引。用法与MaximumRowsParameterName相同。
  SelectCountMethod:这个属性是一个方法的签名,用来告诉ObjectDataSource控件通过什么方式得知数据源中总记录的条数。ObjectDataSource控件同样通过委托来执行这个方法,所以方法的签名必须与属性的值完全一样。
   然后我们在页面上放置一个ListView控件(或者其它任何一个数据绑定控件),将它的DataSourceID属性的值设置为ObjectDataSource的ID,然后添加一个DataPager控件,将PagedControlID属性的值设置为ListView的ID。

0 0
原创粉丝点击