Click here to Skip to main content
15,886,816 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i uses this code but it repeats the same data at every on scroll load please give any idea to solve this prob


my.aspx page

<script type="text/javascript">
    var pageIndex = 1;
    var pageCount;
    $(window).scroll(function () {
        if ($(window).scrollTop() == $(document).height() - $(window).height()) {
            GetRecords();
        }
    });

    $(function () {
        $("[id$=ItemsList] tr").eq(0).remove();
    });

   
                $("[id$=ItemsList]").append(row);

            }
            $.ajax({
                type: "POST",
                url: "Latest-Products-in-greater-noida.aspx/GetLatestProd",
                data: '{pageIndex: ' + pageIndex + '}',
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: OnSuccess,
                failure: function (response) {
                    alert(response.d);
                },
                error: function (response) {
                    alert(response.d);
                }
            });
        }
    }

    //Function to recieve XML response append rows to GridView
    function OnSuccess(response) {
        var xmlDoc = $.parseXML(response.d);
        var xml = $(xmlDoc);
        pageCount = parseInt(xml.find("PageCount").eq(0).find("PageCount").text());
        var Products = xml.find("Products");        
        $("[id$=ItemsList] .loader").remove();
        Products.each(function () {
            var Product = $(this);
            var row = $("[id$=ItemsList] tr").eq(0).clone(true);
            $(".discount", row).html(Product.find("Discount").text());
            $(".product_Id", row).html(Product.find("Product_Id").text());
            $(".product_Name", row).html(Product.find("Product_Name").text());
            $(".print_Price", row).html(Product.find("Print_Price").text());
            $(".selling_Price", row).html(Product.find("Selling_Price").text());
            $(".status", row).html(Product.find("status").text());
            $(".category", row).html(Product.find("Category").text());
            $(".image_Name", row).html(Product.find("Image_Name").text());
            $("[id$=ItemsList]").append(row);
        });

        //Hide Loader
        $("#loader").hide();
    }
</script>
 
                    <asp:DataList ID="ItemsList" BorderColor="black" CellPadding="7" CellSpacing="0"
                        RepeatDirection="Horizontal" RepeatLayout="Table" RepeatColumns="5" BorderWidth="0"
                        runat="server" OnItemCommand="ItemsList_ItemCommand">
                        <ItemTemplate>
                            <div class="latestproduct" style="margin-removed 32px; margin-removed 20px;">
                                <table cellpadding="0" cellspacing="0" class="latestproducttbl">
                                    <tr>
                                        <td style="height: 44px;">
                                            <div class="discountcat">
                                                <table cellpadding="0" cellspacing="0" class="discountcattbl">
                                                    <tr>
                                                        <td style="font-size: 14px; color: White; padding-removed 8px;">
                                                            <%#DataBinder.Eval(Container.DataItem, "Discount")%>%
                                                        </td>
                                                    </tr>
                                                    <tr>
                                                        <td style="color: White;">
                                                            off
                                                        </td>
                                                    </tr>
                                                </table>
                                            </div>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td style="height: 150px;">
                                            <div class="proimage">
                                                <asp:ImageButton ID="ImageButton1" runat="server" CommandName="Details" CommandArgument='<%# Eval("Product_Id") + "," + Eval("Product_Name")%>'
                                                    ImageUrl='<%# Bind("Image_Name", "~/Admin/ProductImage/{0}") %>' Width="140px"
                                                    Height="150px" />
                                            </div>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td style="text-align: center; height: 40px; font-size: 14px; padding: 0px 7px 0px 7px;
                                            vertical-align: top; color: #0875a3;">
                                            <asp:LinkButton ID="LinkButton2" runat="server" CommandName="Details" CommandArgument='<%# Eval("Product_Id") + "," + Eval("Product_Name")%>'
                                                CssClass="lnkpname"><%#DataBinder.Eval(Container.DataItem, "Product_Name")%></asp:LinkButton>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td class="tblprice">
                                            <span class="reduce">Rs.
                                                <asp:LinkButton ID="LinkButton3" runat="server" CommandName="Details" CommandArgument='<%# Eval("Product_Id") + "," + Eval("Product_Name")%>'
                                                    CssClass="reduce"> <%#DataBinder.Eval(Container.DataItem, "Print_Price")%></asp:LinkButton></span>   
                                            Rs.class="sellprice"><asp:LinkButton ID="LinkButton1" runat="server"
                                                CommandName="Details" CommandArgument='<%# Eval("Product_Id") + "," + Eval("Product_Name")%>' CssClass="lnkdtl"
                                                ForeColor="#CD0000"><%#DataBinder.Eval(Container.DataItem, "Selling_Price")%></asp:LinkButton>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td style="text-align: center; height: 50px;">
                                            <asp:LinkButton ID="LinkButton4" runat="server" CommandName="Details" CommandArgument='<%# Eval("Product_Id") + "," + Eval("Product_Name")%>'
                                                CssClass="shopnow">Shop Now</asp:LinkButton>
                                        </td>
                                    </tr>
                                </table>
                            </div>
                        </ItemTemplate>
                    </asp:DataList>




my .cs page

C#
protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ItemsList.DataSource = GetLatestProdPageWise(1);
            ItemsList.DataBind();
        }
    }

  public static DataSet GetLatestProdPageWise(int pageIndex)
    {
        string constring = ConfigurationManager.ConnectionStrings["Grocery"].ConnectionString;
        using (SqlConnection con = new SqlConnection(constring))
        {
            using (SqlCommand cmd = new SqlCommand("[GetLatestProdPageWise]"))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@PageIndex", pageIndex);
                cmd.Parameters.AddWithValue("@PageSize", 20);
                cmd.Parameters.Add("@PageCount", SqlDbType.Int, 4).Direction = ParameterDirection.Output;
                using (SqlDataAdapter sda = new SqlDataAdapter())
                {
                    cmd.Connection = con;
                    sda.SelectCommand = cmd;
                    using (DataSet ds = new DataSet())
                    {
                        sda.Fill(ds, "Products");
                        DataTable dt = new DataTable("PageCount");
                        dt.Columns.Add("PageCount");
                        dt.Rows.Add();
                        dt.Rows[0][0] = cmd.Parameters["@PageCount"].Value;
                        ds.Tables.Add(dt);
                        return ds;
                    }
                }
            }
        }
    }

    [WebMethod]
    public static string GetLatestProd(int pageIndex)
    {
        //Added to similate delay so that we see the loader working
        //Must be removed when moving to production
        System.Threading.Thread.Sleep(2000);

        return GetLatestProdPageWise(pageIndex).GetXml();
    }



my stored procedure

SQL
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE [dbo].[GetLatestProdPageWise]
      @PageIndex INT = 1
      ,@PageSize INT = 20
      ,@PageCount INT OUTPUT
AS
BEGIN
      SET NOCOUNT ON;
      SELECT ROW_NUMBER() OVER
            (
                  ORDER BY [ID] ASC
            )AS RowNumber
      ,[Product_Name]
      ,[Product_Id]
      ,[Category]
      ,[Image_Name]
      ,[Selling_Price]
      ,[Print_Price]
      ,[Discount]
      ,[status]
    INTO #Results
      FROM [Product_Inventory] where (Category='Latest Products') and status='1' Order By Date DESC
    
      DECLARE @RecordCount INT
      SELECT @RecordCount = COUNT(*) FROM #Results
 
      SET @PageCount = CEILING(CAST(@RecordCount AS DECIMAL(10, 2)) / CAST(@PageSize AS DECIMAL(10, 2)))
      PRINT       @PageCount
          
      SELECT * FROM #Results
      WHERE RowNumber BETWEEN(@PageIndex -1) * @PageSize + 1 AND(((@PageIndex -1) * @PageSize + 1) + @PageSize) - 1
    
      DROP TABLE #Results
END
GO
Posted
Updated 19-Mar-15 1:15am
v6

1 solution

The actual problem you're facing is because you're only checking the current location of scroll, you're not having a condition to test whether the data has been loaded or not.

Try this,

JavaScript
var pageIndex = 1;
var pageCount;

// condition for loading data
var loaded = false;
$(window).scroll(function () {
    if ($(window).scrollTop() == $(document).height() - $(window).height() 
        // AND operator to test whether it has been loaded or not
        && !loaded) {
        GetRecords();
        // Set value to true, so it doesn't execute again.
        loaded = true;
    }
});


So, now the variable has been set to true; thus !loaded would result in false and the AND operator would make sure that the code doesn't get executed. Thus, it would load only once; or if you want to it to be executed any other place.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900