【问题标题】:How to assign the data to controls on fly in asp.net如何将数据分配给asp.net中的控件
【发布时间】:2012-10-22 14:23:26
【问题描述】:

我有转发器控件,它将显示 100000 条消息,我不想使用不同的页面向用户显示所有消息。相反,我想在用户向下滚动时加载,而不是加载所有一次发送消息。

例如:Facebook 时间线。

谢谢

编辑:我尝试了什么:

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

            function GetRecords() {
                $("#loader").show();
                $.ajax({
                    type: "POST",
                    url: "Demo.aspx/GetList",
                    data: {},
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: OnSuccess,
                    failure: function (response) {
                        alert("Failure");
                    },
                    error: function (response) {
                        alert("Error");
                    }
                });
            }

            function OnSuccess(response) {
                $.map(response.d, function (item) {
                    $('#division').append("<div style='line-height:25px;'>" + item + "</div>");
                });
                $("#loader").hide();
            }
        });
    </script>

背后的代码

public partial class Demo : System.Web.UI.Page
    {                     
        static List<string> list = new List<string>();
        static int n=0;

        protected void Page_Load(object sender, EventArgs e)
        {
            n = 0;
        }

        public Demo()
        {
            for (int i = 1; i <= 1000; i++)
            {
                list.Add("List Item : " + i);
            }
        }

        [WebMethod]
        public static List<string> GetList()
        {
            var fiftyItems = list.Skip(n).Take(50);
            n = n + 50;
            return fiftyItems.ToList();  
        }
    }

但我想使用更复杂的控制,就像你在 facebook、cmets、图片等中看到的那样。

【问题讨论】:

  • 我会根据this answer更改ajax调用来加载aspx页面(html代码)

标签: c# asp.net


【解决方案1】:

这将是一个相当大的挑战,因为您需要重新绑定转发器控件以显示更多消息。

您可以将转发器嵌入到 UpdatePanel 中,并在用户到达消息末尾时重新绑定。但是您想要显示的消息越多,您需要检索的信息就越多,重新呈现所需的时间就越长。所以可能会有明显的滞后。

您可能希望使用 jQuery 或其他支持 Ajax 的框架在每次您想添加更多消息时从服务器获取另一块消息,并将这些消息添加到您的页面,而无需重新渲染整个转发器的消息.

【讨论】:

    猜你喜欢
    • 2010-10-04
    • 2017-11-09
    • 2014-12-12
    • 1970-01-01
    • 2021-12-23
    • 1970-01-01
    • 2023-01-17
    • 2011-03-18
    • 1970-01-01
    相关资源
    最近更新 更多