문제

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?

도움이 되었습니까?

해결책

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top