简体   繁体   English

使用ajax或jquery进行部分页面加载

[英]Partial page loading using ajax or jquery

I am working on a video portal website, I have completed it almost, but I have some performance issues, ie it takes to much time to load the whole content of the page. 我正在视频门户网站上工作,我几乎完成了工作,但是我遇到了一些性能问题,即加载页面的全部内容需要花费很多时间。

I want to load the header of page immediately and load remaining content using ajax, here is live link to my website. 我想立即加载页面标题并使用ajax加载剩余内容,这是指向我的网站的实时链接。 enter link description here 在此处输入链接说明

Here is code that I am using to load the page 这是我用来加载页面的代码

public partial class Index : System.Web.UI.Page
    {
        PlayListBLL _playList = new PlayListBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!IsPostBack)
                {
                    bindRepeaters();
                }
            }
            catch (Exception) { }
        }

        private void bindRepeaters() //get news of all categoeries.
        {
            rp_top_story.DataSource = _playList.GetData(_playList._topStory).Take(8);
            rp_top_story.DataBind();

            rp_national.DataSource = _playList.GetData(_playList._national).Take(8);
            rp_national.DataBind();

            rp_sports.DataSource = _playList.GetData(_playList._sports).Take(8);
            rp_sports.DataBind();

            rp_entertainment.DataSource = _playList.GetData(_playList._entertainment).Take(8);
            rp_entertainment.DataBind();

            rp_international.DataSource = _playList.GetData(_playList._international).Take(8);
            rp_international.DataBind();

            rp_science_technology.DataSource = _playList.GetData(_playList._scienceTechnology).Take(8);
            rp_science_technology.DataBind();

            rp_smash_hit.DataSource = _playList.GetData(_playList._smashHits).Take(8);
            rp_smash_hit.DataBind();

            rp_food_life_style.DataSource = _playList.GetData(_playList._foodLifeStyle).Take(8);
            rp_food_life_style.DataBind();

            rp_capital_eye.DataSource = _playList.GetData(_playList._capitalEye).Take(8);
            rp_capital_eye.DataBind();
        }
    }

Actually I load header of the page before repeater binding, and repeater should be binds on ajax call. 实际上,我在中继器绑定之前加载页面的页眉,并且中继器应该在ajax调用中进行绑定。

i think you want this code. 我想你想要这个代码。

but i think you must use loading image. 但我认为您必须使用加载图像。 ( this code dose not contains ) (此代码不包含)

and you must use for statement as many as your repeater count. 并且您必须使用for语句作为中继器数量。

because your data loading speed is very slow. 因为您的数据加载速度非常慢。

.aspx .aspx

$.ajax({
    type: "POST",
    url: "/Default.aspx/RequestControlString",
    data: { },
    contentType: "application/json; charset=utf-8",
    dataType : "json",
    success: function (data) {
        $("div").append(decodeURIComponent(data.d));
    }
});

.cs .cs

[System.Web.Services.WebMethod]
public static string RequestControlString()
{
    TextWriter stringWriter = new StringWriter();
    HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);
    var dt = new List<string>() { "1", "2", "3", "4" };
    var gridview = new GridView() { DataSource = dt };
    // you can replace this grid to usercontrol or repeater. 
    // i recommend usercontrol.
    // because you have a style.
    gridview.DataBind();
    gridview.RenderControl(htmlWriter);
    string html = stringWriter.ToString();
    return html;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM