user3134694
user3134694

Reputation: 75

custom paging in repeater

I am trying to apply custom paging in repeater and it works. But when there are only two or three records then it shows me numbers in the bottom.

What I want is, when there are at least 8 to 10 records, then the numbers should be shown in bottom, like 1,2,3 ..in image I indicated through red squares.... here is the image. pic

Here is the code:

protected void BindRepeater()
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["mydms"].ConnectionString.ToString());
    SqlCommand cmd = new SqlCommand("sphrdoc2", con);

    cmd.CommandType = CommandType.StoredProcedure;

    cmd.Parameters.AddWithValue("@UserID", Convert.ToInt32(Session["UserID"]));

    if (con.State == ConnectionState.Closed)
    {
        con.Open();
    }
    DataTable dt = new DataTable();
    SqlDataAdapter adp = new SqlDataAdapter(cmd);
    adp.Fill(dt);
    PagedDataSource pgitems = new PagedDataSource();
    DataView dv = new DataView(dt);
    pgitems.DataSource = dv;
    pgitems.AllowPaging = true;
    pgitems.PageSize = 2;
    pgitems.CurrentPageIndex = PageNumber;
    if (pgitems.PageCount > 1)
    {
        rptPaging.Visible = true;
        ArrayList pages = new ArrayList();
        for (int i = 0; i < pgitems.PageCount; i++)
            pages.Add((i + 1).ToString());
        rptPaging.DataSource = pages;
        rptPaging.DataBind();
    }
    else
    {
        rptPaging.Visible = false;
    }
    Repeater1.DataSource = pgitems;
    Repeater1.DataBind();
}

public int PageNumber
{
    get
    {
        if (ViewState["PageNumber"] != null)
            return Convert.ToInt32(ViewState["PageNumber"]);
        else
            return 0;
    }
    set
    {
        ViewState["PageNumber"] = value;
    }
}
protected void rptPaging_ItemCommand(object source, RepeaterCommandEventArgs e)
{
    PageNumber = Convert.ToInt32(e.CommandArgument) - 1;
    BindRepeater();
}

here is the html

<asp:Repeater ID="rptPaging" runat="server" onitemcommand="rptPaging_ItemCommand">
    <ItemTemplate>
        <asp:LinkButton ID="btnPage" style="padding:8px; margin:2px; background:#20B2AA; border:solid 1px #666; font:8pt tahoma;" CommandName="Page" CommandArgument="<%# Container.DataItem %>" runat="server" ForeColor="White" Font-Bold="True"><%# Container.DataItem %>
        </asp:LinkButton>
    </ItemTemplate>
</asp:Repeater>

Upvotes: 1

Views: 1210

Answers (1)

Raghubar
Raghubar

Reputation: 2788

Add condition check no of rows count in dataview.

        DataView dv = new DataView(dt);
        if{dv.Table.Rows.Count > 10)
        {
             PagedDataSource pgitems = new PagedDataSource();
             pgitems.DataSource = dv;
             pgitems.AllowPaging = true;
             pgitems.PageSize = 2;
             pgitems.CurrentPageIndex = PageNumber;               
             rptPaging.Visible = true;
             ArrayList pages = new ArrayList();
             for (int i = 0; i < pgitems.PageCount; i++)
                   pages.Add((i + 1).ToString());
             rptPaging.DataSource = pages;
             rptPaging.DataBind();


        }
        else
        {
            rptPaging.Visible = false;
        }

Upvotes: 1

Related Questions