2

I have a page with a drop-down. Based on the selection in the drop-down, data gets loaded and populates a RadGrid. I am using a custom user control for the EditTemplate, so I can't use radGrid.DataBind(). Instead, I have to use radGrid.MasterTableView.Rebind() in association with a NeedDataSource event handler.

My problem is that when I load the page initially, I populate the drop-down and automatically select a value (first item in the list) which triggers the databinding on the RadGrid. I can step through the code in debug mode and see that the grid is being populated with data, but when the page displays, it doesn't get rendered. When I then manually choose an item from the drop-down, which triggers the same grid databinding code, it displays properly the second time.

How do I get it to display the grid the first time the page loads?

Mark
  • 200
  • 1
  • 3
  • 8
  • Can you post your code from the NeedDataSource method? – NakedBrunch Mar 17 '10 at 15:57
  • From debugging I've discovered that it has to do with the View State of the grid. When I set EnableViewState = "false" then the grid is displayed on the first page load. However, that totally hoses my user control which is used in the EditForm. – Mark Mar 17 '10 at 20:18
  • Why would disabling the viewstate on the grid cause the grid to properly display the first time the page is loaded? Why would enabling the viewstate on the grid cause the grid to NOT properly display the first time the page is loaded? How can I keep the benefits of having the viewstate enabled, but also display the grid on the first page load? – Mark Mar 17 '10 at 20:19
  • Here is the code from the NeedDataSource method: protected void grdUnits_OnNeedDataSource(object source, GridNeedDataSourceEventArgs e) { grdUnits.DataSource = GridItems; } – Mark Mar 17 '10 at 20:20

2 Answers2

1

I have a very similar issue with nested Multipage with RadGrid in RadGrid

aspx:

<telerik:RadTabStrip><Tabs><!-- ... --></Tabs></telerik:RadTabStrip>
<telerik:RadMultiPage>
<telerik:RadPageView>

<!-- ChildRadGrid1 doesn't display on first time but does on postback --> 
<telerik:RadGrid ID="ChildRadGrid1"><!-- ... --></telerik:RadGrid>

<telerik:RadPageView>
</telerik:RadMultiPage>
</NestedViewTemplate>

<!-- Columns... -->

</MasterTableView>
</telerik:RadGrid>

In my case, only Rebind() in ItemCommand of parent grid helps me:

aspx.cs:

class MyPage : Page
{
  protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
  {
    if (e.CommandName == RadGrid.ExpandCollapseCommandName && e.Item is GridDataItem)
    {
      var dataItem = e.Item as GridDataItem;

      // rebiding fix situation    
      (dataItem.ChildItem.FindControl("ChildRadGrid1") as RadGrid).Rebind();
    }
  }
}
Akim
  • 8,469
  • 2
  • 31
  • 49
0

I can't answer WHY it was happening, but the solution that works for me is to bind the grid to an ObjectDataSource.

<asp:ObjectDataSource ID="gridData" runat="server"/>

I was already binding the grid to a property on the page which was a collection of type List:

protected List<EquipmentGridItem> GridItems { get; set; }

In order to use the ObjectDataSource, I created a wrapper method to return the list.

public object GetGridData()
{
    return GridItems;
}

Then I bound the grid to the object data source.

protected override void OnInit(EventArgs e)
{
    base.OnInit(e);

    grdUnits.DataSourceID = "gridData";
    gridData.TypeName = typeof (ReservationEdit).ToString();
    gridData.SelectMethod = "GetGridData";
}

Kind of a convoluted solution, but it works.

Mark
  • 200
  • 1
  • 3
  • 8