c#asp.net的listview分页问题 我最近自己研究的好玩,做个公司网站,遇到listview分页问题。是在ActList.aspx里显示列表,我用的显示控件是listview,数据源是ObjectDataSource1,设计如下: T_Act表放的是最新活动内容,表字段id, title, ActContent, Author, Province, City, ActTime 单纯的高效分页我会,在强类型dataset里的T_ActTableAdapter建立2个查询,QueryCount()和GetPagedData(startRowIndex,maximumRows)。 QueryCount()的语句为SELECT COUNT(*) FROM T_Act。用来查询总条数。 GetPagedData(startRowIndex,maximumRows)。用来查询分页的数据,的语句为: select * from ( SELECT id, title, ActContent, Author, Province, City, ActTime ,Row_number() over(order by AcTtime desc) as Rcount FROM dbo.T_Act )T where T.Rcount>@startRowIndex and T.Rcount<=@startRowIndex+@maximumRows 这样可以在ActList.aspx里能正确分页。 现在我想根据Province(省)字段按照省来查询,而上面的是查询所有省的数据。 我试过在上面的子查询里加个条件where Province=@province,代码变成这样: select * from ( SELECT id, title, ActContent, Author, Province, City, ActTime ,Row_number() over(order by AcTtime desc) as Rcount FROM dbo.T_Act where Province=@province )T where T.Rcount>@startRowIndex and T.Rcount<=@startRowIndex+@maximumRows 最后生成解决方案后不能运行,错误:必须声明标量变量 "@province"。
我没给@province赋值,我就是想怎么才能在@province赋值? T.Rcount>@startRowIndex and T.Rcount<=@startRowIndex+@maximumRows这是配合DataPager使用的,DataPager内部有着2个属性。
[其他解释]
最好把GetPagedData部分的代码贴出来 建议通过公用变量传进去: string province ="name"; public List<int> GetPagedData(int startRowIndex, int maximumRows) { string sql="where Province='"+province+"'"; ..... }
[其他解释]
GetPagedData(startRowIndex,maximumRows)。是在强类型DataSetAct.xsd的T_ActTableAdapter里创建的查询如下: select * from ( SELECT id, title, ActContent, Author, Province, City, ActTime ,Row_number() over(order by AcTtime desc) as Rcount FROM dbo.T_Act )T where T.Rcount>@startRowIndex and T.Rcount<=@startRowIndex+@maximumRows [其他解释] 具体分页原理我用网上的一个例子: 当数据量非常大的时候,采用ListView内置的分页功能,效率会很低,因为ListView内置的分页功能是当每次请求数据的时候都将所有的数据读出然后选择页面中所请求的数据,因此改用以下方法来提高分页效率。以下方法的原理是:在数据库中进行分页操作,根据所请求的数据来直接将数据库中的数据条数取出,这样每次请求数据的时候,就减轻了服务器的负担。 首先添加数据集查询方法:GetPagedData(在数据库中取得数据分页的方法)和QueryCount(查询所有数据的行数的方法) GetPagedData: select * from ( SELECT Id, Name, Age, Sex,Row_Number() over(order by id) row_number FROM dbo.T_Persons ) t where t.row_number>@startRowIndex and t.row_number<=@startRowIndex+@maximumRows 其中startRowIndex和maximumRows参数需要手动在Parameters属性中设置 QueryCount: SELECT COUNT(*) FROM T_Persons 配置ObjectDataSource,让ListView自动生成Template,然后修改ObjectDataSource的EnablePaging="True",SelectCountMethod设置为取得行数的方法(SelectCountMethod="QueryCount"),删除<SelectParameters>内容。 在<ListView>后面添加<DataPager>: <asp:DataPager ID="DataPager1" runat="server" PagedControlID="ListView1"> <Fields> <asp:NextPreviousPagerField ButtonType="Link" ShowFirstPageButton="True" ShowNextPageButton="False" FirstPageText="首页" /> <asp:NumericPagerField ButtonCount="5" ButtonType="Link" /> <asp:NextPreviousPagerField ButtonType="Link" ShowLastPageButton="True" ShowPreviousPageButton="False" LastPageText="末页" /> </Fields> </asp:DataPager> [其他解释]
假设上例中GetPagedData: select * from ( SELECT Id, Name, Age, Sex,Row_Number() over(order by id) row_number FROM dbo.T_Persons ) t where t.row_number>@startRowIndex and t.row_number<=@startRowIndex+@maximumRows