Paging with datalist

Posted on 2011-05-07
Last Modified: 2012-08-13
I have a datalist to display a list of products. I would like to provide a pager control that can be displayed in the top and bottom of the datalist. Do I need to write this from scratch or is anything availble in .NET?  Please suggest. Thanks
Question by:TrialUser
    LVL 23

    Accepted Solution

    there is already one control... pageddatasource
    LVL 13

    Assisted Solution

    LVL 3

    Assisted Solution

    you can check this:

    even though i suggest you go with ListView (as iHadi Suggested) its much better in this stiuation
    LVL 11

    Assisted Solution

    This is the code for your solution


    <asp:DataList runat="server" id="dlGallery"
        RepeatColumns="4" RepeatDirection="Horizontal">
    <table border="0" cellpadding="0" cellspacing="0">
    <img src="<%#DataBinder.Eval(Container.DataItem,"Image_URL")%>" width="90"
    //Next/Prev Links.
    <table border="0" width="410">
    <td align="left"><asp:LinkButton ID="lbtnPrev"
    <td align="right"><asp:LinkButton ID="lbtnNext"

    //The Code Behind

    private int imgID;
    private string imgTitle;
    private string imgURL;
    private static int pageSize = 8;
    //(This one will hold the no of records return

    //i mean "no. of records per page").

    private static int pageIndex = 0;
    //(This one is for checking the current page).

    public DataSet GetAllImagesCustom(int pageIndex, out int outPageIndex)

    int count = 0;
    DataSet ds = new DataSet();
    ds = //retrieve the data from the database.

    //for paging

    int page = 0;

    //checking the whether the pageIndex value is not <First and >Last.

    //And if it is then assigning the default

    //values for pageIndex and page variables.

    if(((pageIndex-1) <= (ds.Tables[0].Rows.Count/pageSize)) &&
    (pageIndex-1) >= 0)
    //If the pageIndex is >=first and =<last then assigning the start position

    //eg. if pageIndex = 2 then value of 'page' = 8.

    //So in the loop it will add rows to the table

    //from the 8 th row.

    page = pageSize * (pageIndex-1);
    //Assigning default values.

    page = 0;
    pageIndex = 1;

    //creating a data table for adding the required rows or u

    //can clone the existing table.

    DataTable dtImg = new DataTable("Images");
    DataColumn newCol = new DataColumn("Image_ID",Type.GetType("System.Int32"));
    dtImg.Columns.Add(newCol);//For storing image id.

    newCol = new DataColumn("Image_Title",Type.GetType("System.String"));
    dtImg.Columns.Add(newCol);//For storing image Title.

    newCol = new DataColumn("Image_URL",Type.GetType("System.String"));
    dtImg.Columns.Add(newCol);//For storing image URL.

    //adding the required rows to the datatable dtImg.

    foreach(DataRow nRow in ds.Tables[0].Rows)
    //if the page=8 and pageIndex =2 then

    //rows between 8 to 16(if exists) will be added to the new table.

    if(count >= page && count < (pageSize * pageIndex))
    //Adding rows to the datatable 'dtImg'.

    outPageIndex = pageIndex;
    return ds;
    catch(Exception ex)
    throw ex;
    public void BindList()
    DataSet ds = new DataSet();
    ds = GetAllImagesCustom(Convert.ToInt32(txtPageIndex.Text),
    out outPageIndex);
    dlGallery.DataSource = ds;
    //Assigning the new pageIndex value returned from the

    //function to the Hidden textbox.

    txtPageIndex.Text = Convert.ToString(outPageIndex);

    rivate void lbtnPrev_Click(object sender, System.EventArgs e)
    //Actual pageIndex -1

    txtPageIndex.Text = Convert.ToString(Convert.ToInt32(txtPageIndex.Text) - 1);
    private void lbtnNext_Click(object sender, System.EventArgs e)
    //Actual pageIndex +1

    txtPageIndex.Text = Convert.ToString(Convert.ToInt32(txtPageIndex.Text) + 1);

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Introduction The Google Maps API offers various ways to draw dynamic and static maps.  Using a combination of PHP and JavaScript, you can draw active JavaScript maps that allow pan-and-zoom in the client browser window.  You can also draw "static" …
    Use these top 10 tips to master the art of email signature design. Create an email signature design that will easily wow recipients, promote your brand and highlight your professionalism.
    In this tutorial viewers will learn how to position overlapping items using z-index in CSS. They will also learn the restrictions on the z-index property.  Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Red.…
    In this tutorial viewers will learn how to code links for mobile sites that, once clicked, send a call or text to a specified number. For a telephone link (once clicked, calls a number), begin with a normal "<a href=" link tag. For the href, specify…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now