通过ObjectDataSource进行分页的转发器

时间:2009-06-25 20:23:44

标签: c# asp.net

是否可以使用ObjectDataSource实现转发器的自定义分页:

EnablePaging="true" 
MaximumRowsParameterName="maximumRows" 
StartRowIndexParameterName="startRowIndex" 
SelectCountMethod="TotalNumberOfProducts">

或者我必须在代码中设置所有值并传递给ObjectDataSource的参数:

<SelectParameters>
  <asp:Parameter Name="startRowIndex" DbType="Int32" DefaultValue="0" />
  <asp:Parameter Name="maximumRows" DbType="Int32" DefaultValue="10" />
</SelectParameters>

1 个答案:

答案 0 :(得分:0)

由于转发器不支持分页,你无法使用Objectdatasource来实现这一点......
但如果你想做分页,你必须处理代码级...
我在代码级别为datalist分页,因为datalist也不支持分页...
如果您需要代码,我可以为您提供.... 当你想将数据绑定到转发器时调用bindRpt方法,注意:这个例子处理分页下一个,后一个,最后一个,第一个按钮....如果有什么不清楚,请告诉我

private void BindRpt()
{
    PagedDataSource ObjPds;
    ObjPds = DoPagging((DataTable)ViewState["dtblProducts"]);
    dlProducts.DataSourceID = "";
    dlProducts.DataSource = ObjPds;
    dlProducts.DataBind();
}
protected PagedDataSource DoPagging(DataTable dt)
{
    if (dt.Rows.Count > 0)
    {
        lblNoRecord.Visible = false;
        pnlNavigation.Visible = true;
        pnlTopNavigation.Visible = true;
    }
    else
    {
        lblNoRecord.Visible = true;
        pnlNavigation.Visible = false;
        pnlTopNavigation.Visible = false;
    }
    PagedDataSource ObjPagDataSource = new PagedDataSource(); //'declares the paged data source
    ObjPagDataSource.DataSource = dt.DefaultView;

    ObjPagDataSource.AllowPaging = true;
    ObjPagDataSource.PageSize = PgSize;
    if (hdfPageIndex.Value == "SetLastPageIndex")
    {
        CurrentPageIndx = ObjPagDataSource.PageCount - 1;
        ObjPagDataSource.CurrentPageIndex = ObjPagDataSource.PageCount - 1;
    }
    else
    {
        ObjPagDataSource.CurrentPageIndex = CurrentPageIndx; 
    }

    if (ObjPagDataSource.PageCount > 1)
    {
        if (ObjPagDataSource.IsFirstPage == true)
        {
            ibtnLast.Enabled = true;
            ibtnFirst.Enabled = false;
            ibtnPrevious.Enabled = false;  
            ibtnNext.Enabled = true;

            ibtnLastTop.Enabled = true;
            ibtnFirstTop.Enabled = false;
            ibtnPreviousTop.Enabled = false;
            ibtnNextTop.Enabled = true;
        }
        else if (ObjPagDataSource.IsLastPage == true)
        {
            ibtnFirst.Enabled = true;
            ibtnLast.Enabled = false;
            ibtnNext.Enabled = false;
            ibtnPrevious.Enabled = true;

            ibtnFirstTop.Enabled = true;
            ibtnLastTop.Enabled = false;
            ibtnNextTop.Enabled = false;
            ibtnPreviousTop.Enabled = true;
        }
        else
        {
            ibtnFirst.Enabled = true;
            ibtnPrevious.Enabled = true;
            ibtnNext.Enabled = true;
            ibtnLast.Enabled = true;

            ibtnFirstTop.Enabled = true;
            ibtnPreviousTop.Enabled = true;
            ibtnNextTop.Enabled = true;
            ibtnLastTop.Enabled = true;
        }
    }
    else
    {
        ibtnFirst.Enabled = false;
        ibtnLast.Enabled = false;
        ibtnPrevious.Enabled = false;
        ibtnNext.Enabled = false;

        ibtnFirstTop.Enabled = false;
        ibtnLastTop.Enabled = false;
        ibtnPreviousTop.Enabled = false;
        ibtnNextTop.Enabled = false;
    }

    return ObjPagDataSource;
}

public int CurrentPageIndx
{
    get
    {
        System.Object o = this.ViewState["_CurrentPageIndx"];
        if (o == null)
            return 0;
        else
            return Convert.ToInt32(o);
    }
    set
    {
        ViewState["_CurrentPageIndx"] = value;
    }
}

protected void ibtnFirst_Click(object sender, ImageClickEventArgs e)
{
    hdfPageIndex.Value = "";
    CurrentPageIndx = 0;
    BindRpt();
}
protected void ibtnPrevious_Click(object sender, ImageClickEventArgs e)
{
    hdfPageIndex.Value = "";
    CurrentPageIndx = CurrentPageIndx - 1;
    BindRpt();
}
protected void ibtnNext_Click(object sender, ImageClickEventArgs e)
{
    hdfPageIndex.Value = "";
    CurrentPageIndx = CurrentPageIndx + 1;
    BindRpt();
}
protected void ibtnLast_Click(object sender, ImageClickEventArgs e)
{
    hdfPageIndex.Value = "SetLastPageIndex";
    BindRpt();
}