Click here to Skip to main content
15,898,931 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

C#
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.NET
<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?
Posted

1 solution

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:


    C#
    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
        }
    }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900