使用ajax或jquery加载部分页面

时间:2017-03-22 07:49:04

标签: c# jquery asp.net ajax

我正在开发一个视频门户网站,我几乎完成了它,但是我遇到了一些性能问题,即加载页面的整个内容需要花费很多时间。

我想立即加载页面标题并使用ajax加载剩余内容,这里是我网站的实时链接。 enter link description here

以下是我用来加载页面的代码

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();
        }
    }

实际上我在转发器绑定之前加载页面的标题,并且转发器应该在ajax调用上绑定。

1 个答案:

答案 0 :(得分:1)

我想你想要这个代码。

但我认为你必须使用加载图片。 (此代码不包含)

并且必须使用 for statement 和转发器计数一样多。

因为您的数据加载速度非常慢。

的.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

[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;
}