Coolite.Ext.Web动态分页

最近开发需要,用到了Coolite.Ext.Web控件,Coolite封装了大量WEB前台组件,功能确实强大,具体的可以到http://examples.coolite.com/网站体验一下就知道。这个网站给出了大量的例子及源代码,大虾们不要一进去就沉迷其中,还是接着看我的文文吧!

Coolite已经封装了PagingToolBar分页组件,只要设定分页属性,绑定到Store上面就可以实现分页效果。

   笔者开始也是用这种方式进行分页的,开始并不觉得有问题,但是当数据量一大时,打开web页面慢得让人无法忍受。一分析,原来PagingToolBar分页是把数据库中所有的数据全部取出,然后进行分页,如果页面要加载的数据超过200条,指望Coolite自带的分页机制,只有等着吐血吧。

既然已经知道了Coolite分页自带分页机制会加载全部的数据,那么我们为什么要让它加载全部的数据呢?
解决思路:利用存储过程前数据分页,每次只加载传入页码的数据。

所以首先要用存储过程把要显示出来的数据先分页,代码如下:

  1.  create or replace procedure pr_srs_user
  2. (
  3. start in number,
  4. limit in number,
  5. RecoudCount out number,
  6. datatable out Sim_QUERY.cur_query --返回的记录集合
  7. ) as
  8. sqltxt varchar2(4000);
  9. v_sql varchar2(4000);
  10. begin
  11.  sqltxt:='select *
  12.            from SRS_user u';
  13.  sqltxt:='select rownum rn,t.* from ('||sqltxt||') t'
  14.  v_sql:='select count(*) from('||sqltxt||')';
  15.  execute immediate v_sql into RecoudCount;
  16.  v_sql := 'select * from (' || sqltxt || ') where rn between ' ||start1 || ' and ' || limit1;
  17.  open datatable for v_sql;
  18. end;

接着,用业务处理层获取数据,BLL.User类的Bind类方法,代码如下:
public DataSet Bind(int start, int limit, out int cou)
    {
        string conn = "Data Source=;Persist Security Info=True;User ID=;Password=;Unicode=True";
        OracleConnection orconn = new OracleConnection(conn);
        try
        {

            orconn.Open();
            OracleParameter[] pr = new OracleParameter[4];
            pr[0] = new OracleParameter();
            pr[0].ParameterName = "datatable";
            pr[0].OracleType = OracleType.Cursor;
            pr[0].Direction = ParameterDirection.Output;

            pr[1] = new OracleParameter();
            pr[1].ParameterName = "start1";
            pr[1].OracleType = OracleType.Number;
            pr[1].Direction = ParameterDirection.Input;
            pr[1].Value = start;

            pr[2] = new OracleParameter();
            pr[2].ParameterName = "limit1";
            pr[2].OracleType = OracleType.Number;
            pr[2].Direction = ParameterDirection.Input;
            pr[2].Value = limit;

            pr[3] = new OracleParameter();
            pr[3].ParameterName = "RecoudCount";
            pr[3].OracleType = OracleType.Number;
            pr[3].Direction = ParameterDirection.Output;


            OracleCommand cmd = new OracleCommand();
            cmd.CommandText = "pr_srstest_pagesize";
            cmd.CommandType = CommandType.StoredProcedure;
            for (int i = 0; i < 4; i++)
            {
                cmd.Parameters.Add(pr[i]);
            }
            OracleDataAdapter da = new OracleDataAdapter();
            cmd.Connection = orconn;
            da.SelectCommand = cmd;
            da.SelectCommand.ExecuteNonQuery();

            DataSet dt = new DataSet();
            da.Fill(dt);
            cou =Int32.Parse(pr[3].Value.ToString());
 
            orconn.Close();
            return dt;

        }
        catch
        {
            cou = 0;
            return null;
        }
        finally
        {
            orconn.Close();
        }

    }



最近编写显示层的代码:
设定ObjectDataSource,代码如下:

  1. <asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
  2.         SelectMethod="Bind" TypeName="BLL.User"
  3.         onselected="ObjectDataSource1_Selected">
  4.         <SelectParameters>
  5.             <asp:Parameter Name="start" Type="Int32" />
  6.             <asp:Parameter Name="limit" Type="Int32" />
  7.             <asp:Parameter Direction="Output" Name="RecoudCount" Type="Int32" />
  8.         </SelectParameters>
  9.     </asp:ObjectDataSource>

 

增加ObjectDataSourceObjectDataSource1_Selected事件代码:

  1. protected void ObjectDataSource1_Selected(object sender, ObjectDataSourceStatusEventArgs e)
  2.     {
  3.         (this.Store1.Proxy[0] as DataSourceProxy).TotalCount = (int)e.OutputParameters["RecoudCount"];
  4.     }

 

下面要设置Store控件,为Storey设定DataSourceID及增加onrefreshdata事件代码,代码如下:

  1. <ext:Store ID="Store1" runat="server" AutoLoad="true"
  2.             DataSourceID="ObjectDataSource1" onrefreshdata="Store1_RefreshData">
  3.         <Reader>
  4.           <ext:JsonReader>
  5.             <Fields>
  6.               <ext:RecordField Name="OpId_N"></ext:RecordField>
  7.               <ext:RecordField Name="USERID_N"></ext:RecordField>
  8.               <ext:RecordField Name="OPCODE_C"></ext:RecordField>
  9.               <ext:RecordField Name="CONTENT_C"></ext:RecordField>
  10.               <ext:RecordField Name="REMARK_C"></ext:RecordField>
  11.               <ext:RecordField Name="CREATETIME_D"></ext:RecordField>
  12.             </Fields>
  13.           </ext:JsonReader>
  14.         </Reader>
  15.          <AutoLoadParams>
  16.             <ext:Parameter Name="start" Value="={0}"></ext:Parameter>
  17.             <ext:Parameter Name="limit" Value="={20}"></ext:Parameter>
  18.         </AutoLoadParams>
  19.         <Proxy>
  20.             <ext:DataSourceProxy />
  21.         </Proxy>
  22.       </ext:Store>

 

Onrefreshdata事件代码:

  1. protected void Store1_RefreshData(object sender, StoreRefreshDataEventArgs e)
  2.     {
  3.         int limit = Int32.Parse(e.Start.ToString()) + Int32.Parse(e.Limit.ToString());
  4.         int start = Int32.Parse(e.Start.ToString()) + 1;
  5.         GetDataBaind(start.ToString(),limit.ToString());
  6.     }if (start == null)
  7.         {
  8.             start = "0";
  9.         }
  10.         if (limit == null)
  11.         {
  12.             limit = "20";
  13.         }
  14.             this.ObjectDataSource1.SelectParameters["start"].DefaultValue = start;
  15.             this.ObjectDataSource1.SelectParameters["limit"].DefaultValue = limit;
  16.             this.Store1.DataSourceID = "ObjectDataSource1";
  17.             this.Store1.DataBind();
  18.             return;
  19. }

    最后完成。

      

posted @ 2009-07-21 13:20  xiaodi  阅读(2607)  评论(0编辑  收藏  举报