locked
Paging Through a ListView RRS feed

  • Question

  • User-2122576196 posted

    Hey,

    I'm currently pulling groups of 6 rows from my database with my DataPager looking like this:

    <asp:DataPager ID="DataPager1" runat="server" PageSize="4">
                                <Fields>
                                    <asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True" ShowNextPageButton="False" ShowPreviousPageButton="False" />
                                    <asp:NumericPagerField />
                                    <asp:NextPreviousPagerField ButtonType="Button" ShowLastPageButton="True" ShowNextPageButton="True" ShowPreviousPageButton="True" />
                                </Fields>
                            </asp:DataPager>

    When on the first page, it is the front page of my website http://www.darvinkmanwah.com but then when it goes to page 2 of the data the URL does not change. How can I code it where when on page 2 the URL changes to reflect page 2 and so on.

    Tuesday, March 20, 2012 4:48 AM

Answers

  • User79986525 posted

    You Can do like this

    <asp:DataList ID="dlPaging" runat="server" OnItemCommand="dlPaging_ItemCommand" RepeatDirection="Horizontal">

    <ItemTemplate>

     <asp:LinkButton ID="lnkbtnPaging" runat="server" class="paging" CommandArgument='<%#Eval("PageIndex")%>' CommandName="lnkbtnPaging" Text=  '<%#Eval("PageText")%>' ></asp:LinkButton>

    </ItemTemplate>

    </asp:DataList>

     PagedDataSource pds = new PagedDataSource();

       pds.DataSource = ds.Tables[0].DefaultView;
                pds.AllowPaging = true;
                //pds.PageSize = Convert.ToInt16(ddlPageSize.SelectedValue);
                pds.PageSize = 15;
                pds.CurrentPageIndex = CurrentPage;

                //lnkbtnNext.Enabled = !pds.IsLastPage;
                //lnkbtnPrevious.Enabled = !pds.IsFirstPage;

                listproduct.DataSource = pds;
                listproduct.DataBind();

                doPaging();

     protected void dlPaging_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            LinkButton lnkbtnPage = (LinkButton)e.Item.FindControl("lnkbtnPaging");
            if (lnkbtnPage.CommandArgument.ToString() == CurrentPage.ToString())
            {
                lnkbtnPage.Enabled = false;
                lnkbtnPage.Font.Bold = true;

            }
        }

        private void doPaging()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("PageIndex");
            dt.Columns.Add("PageText");
            for (int i = 0; i < pds.PageCount; i++)
            {
                DataRow dr = dt.NewRow();
                dr[0] = i;
                dr[1] = i + 1;
                dt.Rows.Add(dr);
            }

            dlPaging.DataSource = dt;
            dlPaging.DataBind();
        }
        protected void dlPaging_ItemCommand(object source, DataListCommandEventArgs e)
        {
            if (e.CommandName.Equals("lnkbtnPaging"))
            {
                CurrentPage = Convert.ToInt16(e.CommandArgument.ToString());
                BindGrid();
            }
        }

        protected void lnkbtnPrevious_Click(object sender, EventArgs e)
        {
            CurrentPage -= 1;
            BindGrid();
        }

        protected void lnkbtnNext_Click(object sender, EventArgs e)
        {
            CurrentPage += 1;
            BindGrid();
        }

        public int CurrentPage
        {

            get
            {
                if (this.ViewState["CurrentPage"] == null)
                    return 0;
                else
                    return Convert.ToInt16(this.ViewState["CurrentPage"].ToString());
            }

            set
            {
                this.ViewState["CurrentPage"] = value;
            }

        }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, March 21, 2012 8:50 AM

All replies

  • User-330204900 posted

    1. is this relating to Dynmaic Data?

    2. just set the curretn page like this

    GridView1.PageIndex = 2;
    
    Tuesday, March 20, 2012 12:42 PM
  • User-2122576196 posted

    Hey,

    1. Yes, this is Dynamic Data

    2. How do I do that?

    Thanks ...

    Tuesday, March 20, 2012 11:12 PM
  • User-2122576196 posted

    Hey,

    Crazy enough I figured it out (sort of). I simply added a QueryStringField and used 'page' as the word to display, here is what the code looks like.

    <asp:DataPager ID="DataPager1" runat="server" PageSize="5" QueryStringField="page">
                                <Fields>
                                    <asp:NumericPagerField NextPageText="True" RenderNonBreakingSpacesBetweenControls="True" CurrentPageLabelCssClass="heading-15" />
                                </Fields>
                            </asp:DataPager>

    I'm not sure but is it correct? You can check it out here at the bottom of the page http://darvinkmanwah.com 

    Wednesday, March 21, 2012 7:16 AM
  • User-330204900 posted

    all you need to do to set the curretn page number of the pager is

    GridView1.PageIndex = 1;
    

    this needs to be done after Page_Load event. but if your way is working that will be fine.

    Wednesday, March 21, 2012 8:01 AM
  • User-2122576196 posted

    Yah, it works, I'm gonna consider this resolved. I'm also planning on re-writing the URL of each artwork page so it looks cleaner in the URL display instead of a query string ... 

    Thanks for the help by the way ...

    Wednesday, March 21, 2012 8:11 AM
  • User-330204900 posted

    Please do a post on your blog if you have one and link back to this or another of your posts so we can all see how you did it :)

    Wednesday, March 21, 2012 8:41 AM
  • User79986525 posted

    You Can do like this

    <asp:DataList ID="dlPaging" runat="server" OnItemCommand="dlPaging_ItemCommand" RepeatDirection="Horizontal">

    <ItemTemplate>

     <asp:LinkButton ID="lnkbtnPaging" runat="server" class="paging" CommandArgument='<%#Eval("PageIndex")%>' CommandName="lnkbtnPaging" Text=  '<%#Eval("PageText")%>' ></asp:LinkButton>

    </ItemTemplate>

    </asp:DataList>

     PagedDataSource pds = new PagedDataSource();

       pds.DataSource = ds.Tables[0].DefaultView;
                pds.AllowPaging = true;
                //pds.PageSize = Convert.ToInt16(ddlPageSize.SelectedValue);
                pds.PageSize = 15;
                pds.CurrentPageIndex = CurrentPage;

                //lnkbtnNext.Enabled = !pds.IsLastPage;
                //lnkbtnPrevious.Enabled = !pds.IsFirstPage;

                listproduct.DataSource = pds;
                listproduct.DataBind();

                doPaging();

     protected void dlPaging_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            LinkButton lnkbtnPage = (LinkButton)e.Item.FindControl("lnkbtnPaging");
            if (lnkbtnPage.CommandArgument.ToString() == CurrentPage.ToString())
            {
                lnkbtnPage.Enabled = false;
                lnkbtnPage.Font.Bold = true;

            }
        }

        private void doPaging()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("PageIndex");
            dt.Columns.Add("PageText");
            for (int i = 0; i < pds.PageCount; i++)
            {
                DataRow dr = dt.NewRow();
                dr[0] = i;
                dr[1] = i + 1;
                dt.Rows.Add(dr);
            }

            dlPaging.DataSource = dt;
            dlPaging.DataBind();
        }
        protected void dlPaging_ItemCommand(object source, DataListCommandEventArgs e)
        {
            if (e.CommandName.Equals("lnkbtnPaging"))
            {
                CurrentPage = Convert.ToInt16(e.CommandArgument.ToString());
                BindGrid();
            }
        }

        protected void lnkbtnPrevious_Click(object sender, EventArgs e)
        {
            CurrentPage -= 1;
            BindGrid();
        }

        protected void lnkbtnNext_Click(object sender, EventArgs e)
        {
            CurrentPage += 1;
            BindGrid();
        }

        public int CurrentPage
        {

            get
            {
                if (this.ViewState["CurrentPage"] == null)
                    return 0;
                else
                    return Convert.ToInt16(this.ViewState["CurrentPage"].ToString());
            }

            set
            {
                this.ViewState["CurrentPage"] = value;
            }

        }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, March 21, 2012 8:50 AM
  • User-330204900 posted

    have you tested this with Dynamic Data?

    Wednesday, March 21, 2012 8:51 AM
  • User-2122576196 posted

    Hey sanjayverma_mca, Have you tried the code?

    Also just thought I'd share this with you guys ... Hey sjnaughton you were saying the add this "GridView1.PageIndex = 2; to my page but where exactly where you refering to place this code? Thanks 

    This is what I'm using for my reference at the moment for the re-writing of URLs:

    http://weblogs.asp.net/scottgu/archive/2007/02/26/tip-trick-url-rewriting-with-asp-net.aspx

    I've been busy working on some other codes on my site and I haven't gotten to get around to this as yet. You can check out the site over at http://www.darvinkmanwah.com for now and tell me what you think. I made the get alot of code working based on my other posts right here in asp.net.

    You guys rocks ... Thanks ...

    Thursday, April 5, 2012 4:12 AM
  • User79986525 posted

    Hi darvinkmanwah

    Yes I try it even though I am using the code and it's working fine.i hop this code is also working for you and all.

    Saturday, April 7, 2012 12:32 AM
  • User-2122576196 posted

    Thanks alot,

    I ended up using another method and it worked for what I wanted. I simply added a QueryStringField and it worked.

    <asp:DataPager ID="DataPager1" runat="server" PageSize="7" QueryStringField="page">

    You can see the result of the URL here: http://darvinkmanwah.com/digital-playground/artwork/archive.aspx?page=2

    Thanks again for all the help ...

    Saturday, April 7, 2012 1:08 AM
  • User-330204900 posted

    Hey sjnaughton you were saying the add this "GridView1.PageIndex = 2; to my page but where exactly where you refering to place this code? Thanks 
    you place it after Page_Load so OnDataBind is good but also OnPreRender.

    Saturday, April 7, 2012 1:23 PM
  • User-2122576196 posted

    Ok great ... thanks ...

    Check out the page now, it works ... 

    http://darvinkmanwah.com/digital-playground/artwork/archive.aspx?page=2#Top

    Thanks ...

    Tuesday, April 17, 2012 1:54 AM
  • User-564804143 posted

    Hey darvin..

    i tried this into my script its not working for me..on click on next records are not displaying.

    actully i am bin

     <asp:DataPager ID="printlistpaging" runat="server" PagedControlID="LSVAllSlips" PageSize="16" QueryStringField="page">
                <Fields>
                    <asp:NextPreviousPagerField ShowNextPageButton="False" ButtonCssClass="previousNextLink" />
                    <asp:NumericPagerField ButtonCount="10" ButtonType="Link" NumericButtonCssClass="numericLink" />
                    <asp:NextPreviousPagerField ShowPreviousPageButton="False" ButtonCssClass="previousNextLink" />
                </Fields>
            </asp:DataPager>


    ding listview in dropdownchange event..

    Tuesday, May 14, 2013 12:30 AM
  • User-564804143 posted

    url getting change but data is same for all pages...

    i am binding data on dropdownlist changeevent..

    Tuesday, May 14, 2013 12:44 AM
  • User-2122576196 posted

    Hey atul2430,

    Can you insert the link for your page to have a look at, Thanks ...

    Monday, June 24, 2013 1:49 PM