Repeater and the PagedDataSource - Part 2

Repeater and the PagedDataSource - Part 2
Previous Page
1.  In BindPagedDataSource( ):
a. The sdsOrderDetails
    SQLDataSource view is assigned
    as the DataSource of the objPds 
    PagedDataSource.

b. The objPds PagedDataSource is
    assigned as the DataSource of
    the Repeater rptOrderDetails.

c. CurrentPage is assigned a default
    of 0.  It is assigned as the
    CurrentPageIndex of objPds. Its
    value is saved in ViewState.
public int CurrentPage
{
    get
    {
        // look for current page in ViewState
        object o = this.ViewState["_CurrentPage"];
        if (o == null)
            return 0;
        else
            return (int)o;
    }
    set { this.ViewState["_CurrentPage"] = value; }
}

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
        CurrentPage = 0;
    BindPagedDataSource();
}

private void BindPagedDataSource()
{
    DataSourceSelectArguments arg = new DataSourceSelectArguments();
    DataView dv = (DataView)this.sdsOrderDetails.Select(arg);
    PagedDataSource objPds = new PagedDataSource();
    objPds.DataSource = dv;
    objPds.AllowPaging = true;
    objPds.PageSize = 5;
    objPds.CurrentPageIndex = CurrentPage;

    lblCurrentPage.Text = "Page " + (CurrentPage + 1).ToString() + " of "
        + objPds.PageCount.ToString();

    //Enable/Disable Prev and Next buttons
    btnPrev.Enabled = !objPds.IsFirstPage;
    btnNext.Enabled = !objPds.IsLastPage;

    rptOrderDetails.DataSource = objPds;
    rptOrderDetails.DataBind();
    if (objPds.PageCount > 1)
    {
        pnlNavigation.Visible = true;
    }
}

public void btnNext_Click(object sender, System.EventArgs e)
{
    // Set viewstate variable to the next page
    CurrentPage += 1;

    // Reload control
    BindPagedDataSource();
}

public void btnPrev_Click(object sender, System.EventArgs e)
{
    // Set viewstate variable to the previous page
    if (CurrentPage > 0)
        CurrentPage -= 1;
    else
        CurrentPage = 1;

    // Reload control
    BindPagedDataSource();
}

 
2.  Add these paging controls to the
aspx source.

<asp:Panel ID="pnlNavigation" runat="server" Visible="false">
    &nbsp;&nbsp;<asp:Button ID="btnPrev" runat="server" Text=" << " OnClick="btnPrev_Click"
        Height="18px"></asp:Button>&nbsp;&nbsp;
    <asp:Label ID="lblCurrentPage" runat="server"></asp:Label>&nbsp;&nbsp; &nbsp;<asp:Button
        ID="btnNext" runat="server" Text=" >> " OnClick="btnNext_Click" Height="18px">
    </asp:Button>
</asp:Panel>

 
3.  Navigation:
a. btnNext_Click( ) increments
    CurrentPage. It then calls 
    BindPagedDataSource( ). 

b. btnPrev_Click( ) decrements
    CurrentPage. It then calls 
    BindPagedDataSource( ). 

public void btnNext_Click(object sender, System.EventArgs e)
{
    // Set viewstate variable to the next page
    CurrentPage += 1;

    // Reload control
    BindPagedDataSource();
}

public void btnPrev_Click(object sender, System.EventArgs e)
{
    // Set viewstate variable to the previous page
    if (CurrentPage > 0)
        CurrentPage -= 1;
    else
        CurrentPage = 1;

    // Reload control
    BindPagedDataSource();
}

 
4.  rptOrderDetails_ItemDataBound( )
is not a part of the paging
functionality and is optional.

It displays only the first occurrence of
OrderId for orders sharing the same id.
protected void rptOrderDetails_ItemDataBound(object sender, 
                                                                          RepeaterItemEventArgs e)
{
    Panel pnlOrderId = (Panel)e.Item.FindControl("pnlOrderId");
    if (pnlOrderId != null)
    {
        Label lblOrderId = (Label)pnlOrderId.FindControl("lblOrderId");
        if (lblOrderId != null && lblOrderId.Text != _orderId)
        {
            pnlOrderId.Visible = true;
            _orderId = lblOrderId.Text;
        }
    }
}
 
5.  You are done!  Run the app and the repeater will display with paging.
 
Previous Page
 Contact Us     Contact Us     Links     Links      ©2012 GeekPhilosopher.com - All rights reserved
Powered by www.ezjooz.com