vs2008---listview

请注意:因为 DataPager 完全依赖 ListView 执行实际的数据分页,而 ListView 又依赖 DataSource 控件,所以对于其它数据绑定控件也存在相同的分页限制。例如,对于 SqlDataSource 控件,仅当其设置为 DataSet 模式时分页才能正常工作,这意味着需要将整个结果集加载到内存中才能执行分页。当然,您可以使用自定义 DataSource 控件或使用 ObjectDataSource 控件自定义自己的分页。


就来试一下做一小例子.

1. objectdatasource前端代码如下:

 

                <asp:ListView ID="ListView1" runat="server" DataSourceID="ObjectDataSource1">
                    <ItemTemplate>
                        <tr valign="middle" style="height: 20px;">
                            <td>
                                <div class="divNewsListInNewsPage  aNewsOdcSolutions">
                                    <a href='/news/newsview/apjnews/<%# Eval("ID") %>'>
                                        <div><%# Eval("Title")%></div> 
                                    </a>
                                </div>
                            </td>
                            <td style="width: 150px; COLOR: #006096">
                                <div style="float: right;" mce_style="float: right;"> <%# Eval("PublishDate") %></div> 
                            </td>
                        </tr>
                    </ItemTemplate>
                    
                    <LayoutTemplate>
                        <table runat="server" style="width: 100%">
                            <tr runat="server" style="height: 350px;" valign="top">
                                <td runat="server">
                                    <table ID="itemPlaceholderContainer" runat="server" border="0" style="width: 100%">
                                        <tr ID="itemPlaceholder" runat="server">
                                        </tr>
                                    </table>
                                </td>
                            </tr>
                            <tr runat="server">
                                <td runat="server" mce_>
                                    <asp:DataPager ID="DataPager1" runat="server" PageSize='<%$AppSettings:NewsPageSize%>'>
                                        <Fields>
                                            <asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True" 
                                                ShowLastPageButton="True" />
                                        </Fields>
                                    </asp:DataPager>
                                </td>
                            </tr>
                        </table>
                    </LayoutTemplate>
                    
                    <EmptyDataTemplate>
                        <table id="Table1" runat="server">
                            <tr>
                                <td>
                                    未返回数据。</td>
                            </tr>
                        </table>
                    </EmptyDataTemplate>
                </asp:ListView>
                
                    <%--MaximumRowsParameterName="maximumRows"--默认值, 其实就是pageSize--%>
                    <%--StartRowIndexParameterName="startRowIndex"--默认值--%>
                    <%--EnablePaging以前datagrid有个allowpaging的--%>
                <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" 
                    EnablePaging="true"
                    OldValuesParameterFormatString="original_{0}" 
                    SelectMethod="SelectAllDataForNewsPageing" 
                    SelectCountMethod="SelectCountForContentAllInfo" 
                    TypeName="Namespace.Content">
                </asp:ObjectDataSource>

 

SelectMethod是指向的查数据方法名字;

SelectCountMethod是指向查找所有数据总行数的方法;

TypeName是上边两个方法所在的类的full type name.

 

selectParameters可以在前端指定或后端代码指定, 如下:

            this.ObjectDataSource1.SelectParameters.Clear();
            this.ObjectDataSource1.SelectParameters.Add(new Parameter("url", DbType.String, "aaa.aspx"));
            this.ObjectDataSource1.SelectParameters.Add(new Parameter("langCode", DbType.String, bp.FrontLanguage));
            this.ObjectDataSource1.SelectParameters.Add(new Parameter("codedefault", DbType.String, codedefault));
            this.ObjectDataSource1.SelectParameters.Add(new Parameter("maximumRows", DbType.Int32, ConfigurationManager.AppSettings["NewsPageSize"]));
            this.ObjectDataSource1.SelectParameters.Add(new Parameter("startRowIndex", DbType.Int32, "1"));

 

2. SelectMethod和SelectCountMethod的指向的方法代码如下(注意方法的参数和上面的selectParameters)

 

        public int SelectCountForContentAllInfo(string url, string langCode, string codedefault, int maximumRows, int startRowIndex)
        {
            string filterString = " CT_URL='" + url + "' and LangCode='" + langCode + "' ";
            int returnValue = base.SelectCount<ContentAllInfo>(filterString); //base.SelectCount返回数据所有行数
            return returnValue;
        }

 

        public List<ContentAllInfo> SelectAllDataForNewsPageing(string url, string langCode, string codedefault, int maximumRows, int startRowIndex)
        {
            List<ContentAllInfo> list = new List<ContentAllInfo>();
            string filterString = " CT_URL='" + url + "' and LangCode='" + langCode + "' ";
            list = base.SelectData<ContentAllInfo>(maximumRows, startRowIndex + 1, " PublishDate desc ", filterString); //.OrderByDescending(ca => ca.PublishDate).ToList();

            //base.SelectData是根据条件, 每页显示行数参数值maximumRows(以前叫pagesize), 从那一行开始startRowIndex
            if (list.Count == 0 && langCode.ToLower() != codedefault.ToLower())
            {
                filterString = " CT_URL='" + url + "' and LangCode='" + codedefault + "' ";
                list = base.SelectData<ContentAllInfo>(maximumRows, startRowIndex + 1, " PublishDate desc ", filterString);
            }

            return list;
        }

 

3. 根据PageSize和startRowIndex查分页数据的存储过程跟一般一样.

 ALTER PROCEDURE [dbo].[存储过程名字]
  @TableName nvarchar(128),
  @MaxRows int,
  @StartRow int,
  @SortExpression nvarchar(200),
  @FilterString nvarchar(2000)
AS
BEGIN
 -- SET NOCOUNT ON added to prevent extra result sets from
 -- interfering with SELECT statements.
 SET NOCOUNT ON;

    -- Insert statements for procedure here
--  Only for Debug
-- SET @TableName = 'tblPurchaseOrder'
-- SET @MaxRows = 10
-- SET @StartRow = 1
-- SET @SortExpression = 'OrderDate'

 DECLARE @Filter NVARCHAR(2100);
 DECLARE @SqlString NVARCHAR(4000);
 DECLARE @ParmDefinition NVARCHAR(500);

 DECLARE @EndRow int


 SET @EndRow = @MaxRows - 1 + @StartRow

 IF @FilterString is null or @FilterString = ''
  BEGIN
   SET @Filter = ''
  END
  ELSE
  BEGIN 
   SET @Filter = ' WHERE ('+ @FilterString +') '
  END

 /* Specify the parameter format one time. */
 SET @ParmDefinition = N'@StartRowNum int, @EndRowNum int';

  SET @SqlString = N'
   WITH TempTable AS 
   (
    SELECT  *, ROW_NUMBER() OVER (ORDER BY ' + @SortExpression + ') AS RowNum
    FROM  [' + @TableName + '] ' + @Filter + '
   )
   SELECT  *  FROM  TempTable
   WHERE RowNum BETWEEN @StartRowNum AND @EndRowNum
   ORDER BY ' + @SortExpression 
 PRINT @SqlString

  EXECUTE sp_executesql @SqlString
   , @ParmDefinition
   , @StartRowNum = @StartRow
   , @EndRowNum = @EndRow;
END

 

posted on 2012-02-27 17:04 青蛙學堂 阅读(757) 评论(0)  编辑 收藏 引用 所属分类: 數據庫軟件布袋

只有注册用户登录后才能发表评论。
<2006年7月>
2526272829301
2345678
9101112131415
16171819202122
23242526272829
303112345

导航

统计

常用链接

留言簿(8)

随笔分类

随笔档案

收藏夹

青蛙学堂

最新评论

阅读排行榜

评论排行榜