将外部页面加载到手风琴后的延迟 [英] laggy after load external page into accordion

查看:89
本文介绍了将外部页面加载到手风琴后的延迟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用手风琴控制,每个手风琴的内容都是一样的。我不能用HTML构建手风琴,因为手风琴的数量是由数据库中的记录行控制的。 (在这个例子中,我将记录8条记录)



如果我构建手风琴(模板)的内容,代码会很长并且很难维护使用后面的代码,所以我创建一个新页面(accordionTemplate.aspx)并在页面加载时将其加载到每个手风琴。



 < span class =code-keyword> for ( int  i =  0 ; i< 9; i ++)
{
WebRequest request = HttpWebRequest.Create( http:// sampleOnly .com / accordionTemplate.aspx?id = + i.ToString()+ );

WebResponse resonse = request.GetResponse();

StreamReader srReader = new StreamReader(resonse.GetResponseStream());

string HTML = srReader.ReadToEnd();
LiteralControl objControl = new LiteralControl(HTML);

AccordionPane paneIE = new AccordionPane();
paneIE.ID = i.ToString();
paneIE.HeaderContainer.Controls.Add( Title);
paneIE.ContentContainer.Controls.Add(objControl);

AccordionPane1.Controls.Add(paneIE);
}





我的HTML:

 <   asp:Accordion     ID   =  Accordion2    runat   =  server    SelectedIndex   =   -  1    RequireOpenedPane   =  false >  
< 窗格 >
< asp:AccordionPane ID = AccordionPane1 runat = server >
< / asp:AccordionPane >
< / Panes >
< / asp:Accordion >





上面的示例正在工作,但唯一的问题是,当我在页面内滚动或展开时,我的页面变得非常迟钝或手风琴。任何解决方案?

解决方案

您正在为您查询的每条记录创建一个Web请求,您正在创建而不是处置非托管资源,所有您的数据提取正在服务器端完成。你看到硬延迟就不足为奇了。



你的主要问题是:你的代码在服务器上运行,因此每当你触发手风琴时你都是:



  1. 向服务器发送请求。
  2. 让服务器向自己发送9个同步Web请求... 。但是,由于它使用DNS,它必须走向世界与自己交谈。
  3. 执行对数据请求的任何操作并将其流回自身。至少它应该只需要到最近的托管交换机或路由器这一次回到自己。
  4. 将它自己请求的信息从它自己运行到你的AccordionPane控件。
  5. 渲染并发送回客户端
  6. 如果所有窗格都相同并且您不想学习运行jQueryUI手风琴所需的基本jQuery(总是更有效地在演示层上保持演示),我建议您创建一次控件,获取一次关联数据,然后在循环中复制控件并使用您的数据填充它。



    并处置您的非托管资源,如果您在重构后仍然需要它们。即使它很简单:





     WebRequest request = HttpWebRequest.Create(  http://sampleOnly.com/accordionTemplate.aspx?id= + i.ToString()+  ); 
    使用(WebResponse resonse = request.GetResponse())
    {
    using (StreamReader srReader = new StreamReader(resonse.GetResponseStream()))
    {
    // 在此处捕获流数据和格式控制
    }
    }


I'm using accordion control, and the content of each accordion is the same. i cannot build the accordion at HTML, because the amount of accordion is control by the row of records from database. (at this example, i'll take 8 records)

the code would be very long and hard to maintenance if i build the content of the accordion(template) by using code behind, so i create a new page (accordionTemplate.aspx) and load it into each accordion on page load.

for(int i=0; i<9;i++)
{
   WebRequest request = HttpWebRequest.Create("http://sampleOnly.com/accordionTemplate.aspx?id=" + i.ToString() + "");

    WebResponse resonse = request.GetResponse();

    StreamReader srReader = new StreamReader(resonse.GetResponseStream());

    string HTML = srReader.ReadToEnd();
    LiteralControl objControl = new LiteralControl(HTML);

    AccordionPane paneIE = new AccordionPane();
    paneIE.ID = i.ToString();
    paneIE.HeaderContainer.Controls.Add("Title");
    paneIE.ContentContainer.Controls.Add(objControl);

    AccordionPane1.Controls.Add(paneIE);
}



my HTML:

<asp:Accordion ID="Accordion2" runat="server" SelectedIndex="-1" RequireOpenedPane="false">
   <Panes>
      <asp:AccordionPane ID="AccordionPane1" runat="server">
      </asp:AccordionPane>
   </Panes>
</asp:Accordion>



the example above is working, but the only issue is that my page become very laggy while im scrolling within the page or expand or accordion. any solution for this?

解决方案

You're creating a web request for every single record that you're querying, you're creating and not disposing unmanaged resources, all your data fetching is being done on the server side. It's not surprising that you're seeing hard lag.

Your primary issue is this: your code behind runs on the server and so every time you trigger that accordion you are:


  1. Sending a request to the server.
  2. Having the server send 9 synchronous web requests...to itself. But since it's using DNS it has to go out to the world to talk to itself.
  3. Perform whatever operations are requested for the data and stream it back to itself. At least it should only have to go to the closest managed switch or router to get back to itself this time.
  4. Run the information that it just web requested from itself into your AccordionPane control.
  5. Render and send back to the client
  6. If all the panes are the same and you don't want to learn the basic jQuery needed to run a jQueryUI accordion (always more effective to keep presentation on the presentation layer), I suggest that you create the control once, get the associated data once, and copy the control in your loop and populate it with your data.

    And dispose your unmanaged resources, if you still need them after refactoring. even if it's as simple as:


    WebRequest request = HttpWebRequest.Create("http://sampleOnly.com/accordionTemplate.aspx?id=" + i.ToString() + "");
    using(WebResponse resonse = request.GetResponse())
    {
        using(StreamReader srReader = new StreamReader(resonse.GetResponseStream()))
        {
            //Capture stream data and format control here
        }
    }


这篇关于将外部页面加载到手风琴后的延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆