Site icon port135.com

Solved: Telerik RadGrid doesn’t retain data after a postback

You may come across this issue when you try to sort, filter or group your table that is built with Telerik RadGrid component. Each of these sorting, filtering, and grouping features trigger a postback. You may not see the data in your table after the postback is completed.

Solution

Make sure you define OnNeedDataSource attribute for your RadGrid object and handle NeedDataSource event. Telerik uses NeedDataSource event each time RadGrid is need to be bound with a data source.

ASPX file:

<telerik:RadGrid RenderMode="Lightweight" ID="RadGrid1" runat="server" CellSpacing="0"
GridLines="None" OnNeedDataSource="RadGrid1_NeedDataSource1" AllowPaging="true" PageSize="10">
     <MasterTableView AutoGenerateColumns="true" DataKeyNames="CustomerID">
     </MasterTableView>
</telerik:RadGrid>

Code-behind file:

protected void RadGrid1_NeedDataSource1(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
{
      RadGrid1.DataSource = GetDataTable("SELECT CustomerID, CompanyName, ContactName FROM Customers");
}

References:

Exit mobile version