1

我想测试这里找到的代码。它允许我在用户向下滚动时从服务器自动加载内容。

我在尝试将代码转换为 C#/ASP.NET 时遇到了困难。首先,我希望将此代码转换为 C#/ASP.NET:

package com.vraa.demo;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class InfinitContentServlet extends HttpServlet {
    private static Integer counter = 1;

    protected void processRequest(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        try {
            String resp = "";
            for (int i = 1; i <= 10; i++) {
                resp += "<p><span>"
                        + counter++
                        + "</span> This is the dynamic content served freshly from server</p>";
            }
            out.write(resp);
        } finally {
            out.close();
        }
    }

    @Override
    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        processRequest(request, response);
    }
}

此外,我想知道是否必须对此 JavaScript 代码进行任何更改:

<script type="text/javascript">
        $(document).ready(function(){
            $contentLoadTriggered = false;
            $("#content-box").scroll(function(){
                if($("#content-box").scrollTop() >= ($("#content-wrapper").height() - $("#content-box").height()) && $contentLoadTriggered == false)
                {
                    $contentLoadTriggered = true;
                    $.get("infinitContentServlet", function(data){
                        $("#content-wrapper").append(data);
                        $contentLoadTriggered = false;
                    });
                }

            });
        });
    </script>
4

1 回答 1

1

我认为类似的概念..(作为处理程序.ashx)

using System;
using System.Web;

public class InfinitContentHandler : IHttpHandler {
    private static Int32 counter = 1;

    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";        

        for (int i = 1; i <= 10; i++) {
            context.Response.Write("<p><span>" + counter++ + "</span>");
            context.Response.Write("This is the dynamic content served freshly from server</p>");
        }

    }

    public bool IsReusable {
        get {
            return false;
        }
    }

}
于 2010-11-11T23:06:28.983 回答