views:

223

answers:

4

Hi

I am dynamically creating a table of checkboxes on my test.aspx page. The dimensions (row and column count) of the table are determined by querying the database.

In test.aspx page, I do the following:

<script language="C#" runat="server">

protected void Page_Load ( object src, EventArgs e ) {
   //Query the database to get the data for rows and columns

   CheckBox[] chkBox = new CheckBox[rows * columns]; //creates a collection of checkboxes

   //Iterate over the query set in a double for loop to create dynamic checkboxes
}

</script>

Right now I have the number of rows=20 and columns=10, but it can increase (with the columns more likely).

I used the Net tab in Firebug to ascertain the time taken by different events and found that GetTest.aspx is taking close to 4 minutes, which is too long a time to wait.

Is there a way to being down the page load time? Is my approach to create dynamic check boxes correct?

Thanks in advance.

cheers

A: 

Creation of controls (CheckBox) and adding to a holder from the server-side is very inexpensive. Considering you are not creating billions.

The HTML that is generated should not be big enough to take 4 minutes on a local machine.

Please check the generated HTML size to verify its mass.

If I were you then I would have written the following code on my server. Please consider.

protected void Page_Load(object src, EventArgs e) {
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < columns; j++) {
            holderPanel.Controls.Add(
                    new CheckBox { 
                            ID = string.Format("chk{0}{1}", i, j), 
                            Text = "some text"
            });
        }
    }
}

Consider the holderPanel is a server side asp:Panel or a simple Div with ID = "holderPanel" and runat="server"

Munim Abdul
+1  A: 

Enabling ASP.NET trace on the page and see where all the time is spent. Four minutes is of course way too long for any page. You list two though... test.aspx and GetTest.aspx... what is GetTest.aspx?

EDIT:
OK, you are not telling us the whole story here. What else is this page doing? Where are these controls going? I just tried this on a test page using code similar to that above and it renders in a split second.

Like I said... enable TRACE and find out what is really taking up all the time! Use the tool, that's why it's there.

Bryan
GetTest.aspx is the label on the Net tab of Firebug showing the time taken to GET the Test.aspx page.
Andriyev
Gotcha. See my edits.
Bryan
I would lay odds that the problem is not in the checkbox generation, but rather in the db layer.
Chris Lively
A: 

Try disabling Firebug & see if it still takes that long. Also double check that your code didn't generate more checkboxes than you expected.

Noel
+1  A: 

I'm looking at this comment:

// Query the database to get the data for rows and columns

You gloss over this, but 9 times out of 10 when a web page loads slowly it's because it's performing some slow database operation.

My guess is that either (a) you have a very inefficient database query, perhaps due to a lack of indexing, or (b) you're running a database query inside a loop somewhere (very bad).

ASP.NET can create thousands of checkboxes in less than 1 second. It's just class instantiation. The issue is somewhere else.

Aaronaught