使用ObjectDataSource 控件自定义自己的分页(vs2008新控件Listview)

在看贴子http://msdn.microsoft.com/zh-cn/magazine/cc337898.aspx 时,里边有一段话,如下:

 

请注意:因为 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" style="text-align: right" mce_style="text-align: right">
                                    <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" style="">
                            <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

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值