Coolite.Ext.Web动态分页
Coolite已经封装了PagingToolBar分页组件,只要设定分页属性,绑定到Store上面就可以实现分页效果。
笔者开始也是用这种方式进行分页的,开始并不觉得有问题,但是当数据量一大时,打开web页面慢得让人无法忍受。一分析,原来PagingToolBar分页是把数据库中所有的数据全部取出,然后进行分页,如果页面要加载的数据超过200条,指望Coolite自带的分页机制,只有等着吐血吧。
既然已经知道了Coolite分页自带分页机制会加载全部的数据,那么我们为什么要让它加载全部的数据呢?
解决思路:利用存储过程前数据分页,每次只加载传入页码的数据。
所以首先要用存储过程把要显示出来的数据先分页,代码如下:
-
create or replace procedure pr_srs_user
-
(
-
start in number,
-
limit in number,
-
RecoudCount out number,
-
datatable out Sim_QUERY.cur_query --返回的记录集合
-
) as
-
sqltxt varchar2(4000);
-
v_sql varchar2(4000);
-
begin
-
sqltxt:='select *
-
from SRS_user u';
-
sqltxt:='select rownum rn,t.* from ('||sqltxt||') t';
-
v_sql:='select count(*) from('||sqltxt||')';
-
execute immediate v_sql into RecoudCount;
-
v_sql := 'select * from (' || sqltxt || ') where rn between ' ||start1 || ' and ' || limit1;
-
open datatable for v_sql;
-
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,代码如下:
-
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
-
SelectMethod="Bind" TypeName="BLL.User"
-
onselected="ObjectDataSource1_Selected">
-
<SelectParameters>
-
<asp:Parameter Name="start" Type="Int32" />
-
<asp:Parameter Name="limit" Type="Int32" />
-
<asp:Parameter Direction="Output" Name="RecoudCount" Type="Int32" />
-
</SelectParameters>
- </asp:ObjectDataSource>
增加ObjectDataSource的ObjectDataSource1_Selected事件代码:
-
protected void ObjectDataSource1_Selected(object sender, ObjectDataSourceStatusEventArgs e)
-
{
-
(this.Store1.Proxy[0] as DataSourceProxy).TotalCount = (int)e.OutputParameters["RecoudCount"];
-
}
下面要设置Store控件,为Storey设定DataSourceID及增加onrefreshdata事件代码,代码如下:
-
<ext:Store ID="Store1" runat="server" AutoLoad="true"
-
DataSourceID="ObjectDataSource1" onrefreshdata="Store1_RefreshData">
-
<Reader>
-
<ext:JsonReader>
-
<Fields>
-
<ext:RecordField Name="OpId_N"></ext:RecordField>
-
<ext:RecordField Name="USERID_N"></ext:RecordField>
-
<ext:RecordField Name="OPCODE_C"></ext:RecordField>
-
<ext:RecordField Name="CONTENT_C"></ext:RecordField>
-
<ext:RecordField Name="REMARK_C"></ext:RecordField>
-
<ext:RecordField Name="CREATETIME_D"></ext:RecordField>
-
</Fields>
-
</ext:JsonReader>
-
</Reader>
-
<AutoLoadParams>
-
<ext:Parameter Name="start" Value="={0}"></ext:Parameter>
-
<ext:Parameter Name="limit" Value="={20}"></ext:Parameter>
-
</AutoLoadParams>
-
<Proxy>
-
<ext:DataSourceProxy />
-
</Proxy>
-
</ext:Store>
Onrefreshdata事件代码:
-
protected void Store1_RefreshData(object sender, StoreRefreshDataEventArgs e)
-
{
-
int limit = Int32.Parse(e.Start.ToString()) + Int32.Parse(e.Limit.ToString());
-
int start = Int32.Parse(e.Start.ToString()) + 1;
-
GetDataBaind(start.ToString(),limit.ToString());
-
}if (start == null)
-
{
-
start = "0";
-
}
-
if (limit == null)
-
{
-
limit = "20";
-
}
-
this.ObjectDataSource1.SelectParameters["start"].DefaultValue = start;
-
this.ObjectDataSource1.SelectParameters["limit"].DefaultValue = limit;
-
this.Store1.DataSourceID = "ObjectDataSource1";
-
this.Store1.DataBind();
-
return;
-
}
最后完成。