开发者

Telerik RadGrid doesn't display on first Page_Load but does on postback

开发者 https://www.devze.com 2022-12-23 11:40 出处:网络
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

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 开发者_如何学Cpage loads?


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();
    }
  }
}


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消