locked
DataList PagedDataSource PageCount Number incorrect. RRS feed

  • Question

  • User-27405682 posted

    I has 24items, when first load my page count is get correct which is 4 pages.

    But second  and third times click on next button the page count show 2. Suppose my page count will be deduct by one when each times click.

            private void getItems(int value)
            {
                productCatalog.productCatalog client = new productCatalog.productCatalog();
                string jsonData = client.getDataInJsonFormat(value);
                JavaScriptSerializer js = new JavaScriptSerializer();
                IList<DtoProject> dt = JsonConvert.DeserializeObject<List<DtoProject>>(jsonData);
    
                PagedDataSource objPds = new PagedDataSource();
    
               
                objPds.AllowPaging = true;
                objPds.PageSize = dataSizePreview;
                objPds.DataSource = dt;
                objPds.CurrentPageIndex = CurrentPage;
                PageCount = objPds.PageCount;
                int a = PageCount;
               
                
    
                lblCurrentPage.Text = "Page: " + (CurrentPage +1 ).ToString() + " of "
                + PageCount + " and " + dt.Count + " and " + objPds.Count;
    
                // Disable Prev or Next buttons if necessary
                // cmdNext.Enabled = !objPds.IsLastPage;
                // cmdNext.Visible = !objPds.IsLastPage;
                if (cmdNext.Enabled == objPds.IsLastPage)
                {
                    cmdNext.Attributes.CssStyle.Add("text-decoration", "none;");
                    cmdNext.Text = "You have reached the end. \n Do a search to keep exploring!";
                    cmdNext.Enabled = false;
                }
    
                DatalistProduct.DataSource = objPds;
                DatalistProduct.DataBind();
    
            }
            protected int PageCount
            {
                get
                {
                    // look for current page count in ViewState
                    object o = this.ViewState["_PageCount"];
                    if (o == null)
                        return 1;   // default to just 1 page
                    else
                        return (int)o;
                }
    
                set
                {
                    this.ViewState["_PageCount"] = value;
                }
            }
    
    
            public int CurrentPage
            {
                get
                {
                    // look for current page in ViewState
                    object o = ViewState["_CurrentPage"];
                    if (o == null)
                        return 0; // default to showing the first page
                    else
                    return (int)o;
                }
                set
                {
                    ViewState["_CurrentPage"] = value;
                }
            }
    
    
            protected void cmdNext_Click(object sender, EventArgs e)
            {
                dataSizePreview = dataSizePreview + dataSize;
                // Reload control
                getItems(Convert.ToInt32(sortBy.SelectedValue));
            }
    
         
    Friday, August 31, 2018 2:15 PM

Answers

  • User475983607 posted

    Rather than a community member needing to copy your code to setup a testing app, it is much more efficient if you take the time to set a breakpoint and debug the code.

    Just looking at the code brings up a lot of design questions...  For example these bits.

    Here you are creating a new data source and assigning the PageCount property to the PAgedDataSource.PageCount property.  I assume the value is zero at this point.  

                PagedDataSource objPds = new PagedDataSource();
    
               
                objPds.AllowPaging = true;
                objPds.PageSize = dataSizePreview;
                objPds.DataSource = dt;
                objPds.CurrentPageIndex = CurrentPage;
                PageCount = objPds.PageCount;
                int a = PageCount;

    The only time the page count should ever change is if the number of records per page change or a new result set is returned.  Which bring us to this event.

            protected void cmdNext_Click(object sender, EventArgs e)
            {
                dataSizePreview = dataSizePreview + dataSize;
                // Reload control
                getItems(Convert.ToInt32(sortBy.SelectedValue));
            }

    The code is adding to the dataSizePreview but there is not indication how dataSizePreview is populated in the first place.  Likewise,  there is no indication where dataSize is set.  You're passing an int to the getItems which looks like a column sort but it is used to fetch records.

    I cannot wrap my head around the intended design.  That's why I recommend you debug the code as only you know how the code is supposed to work.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 31, 2018 3:55 PM