使用linq to sql和Web方法单击按钮时从数据库中获取接下来的8行

本文关键字:数据库 获取 接下来 8行 按钮 sql to linq Web 单击 方法 | 更新日期: 2023-09-27 18:34:42

>我正在创建一个网页,在该网页中,我在页面加载时从数据库中获取前 8 行。我在网页底部放置了"加载更多"按钮。我想要的是当我单击加载更多按钮时,它会显示下一个新的 8 行并跳过以前的记录,如果没有找到新记录,则什么也不显示。下面是我正在尝试的代码,但它重复了相同的重复记录。

//Below event is fetching top 8 rows on page load
    function viewAllEvents() {
        $.ajax({
            url: "Event.aspx/viewEvents",
            data: null,
            contentType: "Application/json; charset=utf-8",
            responseType: "json",
            method: "POST",
            success: function (response) {
                var x = response.d;
                for (var i = 0; i < x.length; i++) {
                    $("#tabss > .event-container > .row").append(
                            "<div class='col-md-3'><div class='event'><div class='eventsimg'><img src= " + '../MediaUploader/' + x[i].EVE_IMG_URL + " alt=''></div><div class='event-content'><h3 class='title'>" + x[i].EVE_NAME + "</h3><p>" + x[i].EVE_DESCRIPTION + "</p><input type='button' id=" + i + " class='btn btn-pri' style='padding: 9px 9px;font-size: 12px;' onClick='eveReq(" + i + ", " + x[i].ID + ", " + x[i].EVE_CAT_ID + ");' value='Send Request' /><input type='button' class='btn btn-pri' style='padding: 9px 9px;font-size: 12px;margin-left: 2px;' value='Read More' /></div><div class='links clearfix'></div></div></div>"
                        );
                }
            },
            error: function (xhr) {
                alert(xhr.status);
            },
            Failure: function (response) {
                alert(response);
            }
        });
    }
//Below event is for when load more button is clicked
    function addTabs() {
        $.ajax({
            url: "Event.aspx/addTab",
            data: null,
            contentType: "Application/json; charset=utf-8",
            responseType: "json",
            method: "POST",
            success: function (response) {
                var x = response.d;
                for (var i = 0; i < x.length; i++) {
                    $("#tabss > .event-container > .row").append(
                            "<div class='col-md-3'><div class='event'><div class='eventsimg'><img src= " + '../MediaUploader/' + x[i].EVE_IMG_URL + " alt=''></div><div class='event-content'><h3 class='title'>" + x[i].EVE_NAME + "</h3><p>" + x[i].EVE_DESCRIPTION + "</p><input type='button' id=" + i + " class='btn btn-pri' style='padding: 9px 9px;font-size: 12px;' onClick='eveReq(" + i + ", " + x[i].ID + ", " + x[i].EVE_CAT_ID + ");' value='Send Request' /><input type='button' class='btn btn-pri' style='padding: 9px 9px;font-size: 12px;margin-left: 2px;' value='Read More' /></div><div class='links clearfix'></div></div></div>"
                        );
                }
            },
            error: function (xhr) {
                alert(xhr.status);
            },
            Failure: function (response) {
                alert(response);
            }
        });
    }

以下是我的网络方法:

[WebMethod]
public static List<EVENT> viewEvents()
{
     var slist = new List<EVENT>();
     var db = new BLUEPUMPKINEntities();
     db.Configuration.LazyLoadingEnabled = false;
     slist = db.EVENTS.OrderByDescending(eve => eve.ID).Take(8).ToList();
     return slist;
}
[WebMethod]
public static List<EVENT> addTab()
{
    var slist = new List<EVENT>();
    var db = new BLUEPUMPKINEntities();
    db.Configuration.LazyLoadingEnabled = false;
    slist = db.EVENTS.OrderByDescending(eve => eve.ID).Skip(8).Distinct().ToList();
    return slist;
}

使用linq to sql和Web方法单击按钮时从数据库中获取接下来的8行

虽然我没有使用你的代码,但我相信如果你能通过下面的这个例子,那么你就会知道该怎么做:

首先声明一个全局变量,该变量将计算到目前为止返回的记录:

private int recordCount = 0;

然后在单击事件中执行以下操作:

 //My sample data 
 int[] data = { 1, 2, 3, 4, 5, 6, 7, 8 };
 var results = data.Skip(recordCount).Take(2);
 //Increment recordCount by the count of the results return above
 recordCount+= results.Count();
 if (results.Count() > 0)
 {
    //return results 
 }