views:

17

answers:

2

I have a ASP.NET page using a PlaceHolder. Grids are build programmatically and added to this PlaceHolder when the page is run. Example:

ASPX Code:

<asp:PlaceHolder ID="myPlaceHolder" runat="server" />

Code behind:

foreach (var country in Tables.Countries())
{
  var nGrid = BuildGrid(country.Code);
  if (nGrid.Rows.Count > 0)
  {
      var lTitle = new Literal();
      lTitle.Text = "<h2>Stats for country " + country.Name + "</h2>";

      myPlaceHolder.Controls.Add(lTitle);
      myPlaceHolder.Controls.Add(nGrid);
  }
  nGrid.Dispose();
}

private GridView BuildGrid(short countryCode)
{
    GridView nGrid = new GridView();
    nGrid.ID = "gr_" + countryCode;
    nGrid.SkinID = "rpSkin";
    nGrid.AutoGenerateColumns = false;
    nGrid.AllowPaging = false;
    nGrid.AllowSorting = false;
    nGrid.RowStyle.VerticalAlign = VerticalAlign.Top;
    nGrid.EnableViewState = false;

    var nField = new BoundField
                                 {
                                     HeaderText = "People",
                                     DataField = "PeopleCount"
                                 };
                nGrid.Columns.Add(nField);

    // more BoundFields of this type exist

    // This is basically the GridViewHelper class that gets Row Totals
    // Disabling this doesn't help, either
    HelpGrid(nGrid);
    nGrid.DataSource = Country.GetPeople(countryCode);
    nGrid.DataBind();
    return nGrid;
}

This page works flawlessly in Opera and Firefox. Internet Explorer 8 shows me the "cannot display the webpage" screen.

Any ideas?

A: 

You're always going to run into a ton of problems when programmatically creating controls. It may work on get, but not on post, you have issues in OnClick handlers, etc.. there's a lot of hoops you have to jump through to make programmatic controls work right.

It really doesn't look to me like you absolutely need to programmatically create these. You could just as easily create the grid control as a user control, then pass your DataSource through. If need be, you can load the user controls at runtime, and you cut out the whole build the grid dynamiclaly part and let asp.net take care of the mess.

This is not to say you can't do it, as people do all the time. I'm just suggesting that you're setting yourself up for a lot more work to do it the way you are.

Mystere Man
There are multiple reasons why I need this to be created programmatically. Anyhow, the issue is that the code above works with Firefox and Opera, but not with IE.
Jim
All that means is that it's probably generating invalid HTML somehow, or that the size of the data is causing a timeout.
Mystere Man
I zoned it down to IE somehow not liking BoundFields added programmatially to the GridView! Commenting out the nGrid.Columns.Add(nField); atleast shows the page! Still exploring...
Jim
Check the output HTML in Firefox and see if it looks valid or not.
Mystere Man
A: 

Could this be your issue?

Druid