Which event does RadGrid set its control state?

1 Answer 24 Views
Grid
Chris
Top achievements
Rank 1
Iron
Chris asked on 13 Mar 2025, 03:29 AM

I have encountered a niche issue I'm trying to solve. I have a grid with EnableViewState set to false. Based on this, the control state within the grid is used to manage things like sort expressions.

My issue is simply this:

  1. Page load
  2. Programmatically add columns to a grid
  3. On the UI, sort on a column, and its name for example is 'My Column', this becomes 'My Column ASC'
  4. On our app are objects in which its name we can add as a column to the same grid
  5. Somewhere in our app I rename the object's name to something else like 'My New Column' - note the sort expression is still 'My Column ASC'
  6. Back to the grid, I cause a postback (e.g., rebind) or reorder a column, or sort, etc.
  7. The columns need to be re-added (I've covered a case to exclude the column with the old name)
  8. Before columns are added again, I manually clear sort expressions on the grid
  9. While debugging, I found that in the ColumnCreated event, the grid sort expressions restore to what they were before the postback, and I see 'My Column ASC' - I find that whenever adding a SortExpression referencing a non-existing column, the program always breaks

 

I debugged against a few events of the grid and found that in ColumnCreated (after NeedDataSource) the grid restored its sort expressions from what I presume is the control state. So, this was between NeedDataSource and ColumnCreated.

 

When exactly did the sort expressions get restored, and is there an event I can listen to?

1 Answer, 1 is accepted

Sort by
0
Vasko
Telerik team
answered on 17 Mar 2025, 08:05 AM

Hello Chris,

I hope you're doing well!

In the Grid lifecycle, sort expressions are restored from the control state during the OnLoad event. This occurs before the NeedDataSource event is triggered. The restoration ensures that essential state information, such as sort expressions, is available early in the page lifecycle.

Events to Manage Sort Expressions

  • NeedDataSource Event: This event is triggered before data binding occurs. It is an ideal place to clear or modify sort expressions before the grid binds data. Since the control state is restored before this event, you can perform necessary adjustments to sort expressions to prevent issues with non-existing columns.

  • ColumnCreated Event: As you've discovered, this event fires after NeedDataSource and is where you notice the sort expressions being restored. You can use this event to validate and adjust sort expressions if needed.

Suggested Approach

To handle scenarios where column names change and sort expressions become invalid:

  • Clear Sort Expressions in NeedDataSource: Before dynamically adding columns, clear the sort expressions to prevent invalid references.
protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
    RadGrid grid = (RadGrid)sender;
    grid.MasterTableView.SortExpressions.Clear();
}

By managing sort expressions in the NeedDataSource and ColumnCreated events, you can address issues related to invalid column references and ensure the grid's functionality remains intact during postback.

Try this and see if it will help you out.

Please refer to the following articles for additional information:

Regards,
Vasko
Progress Telerik

Enjoyed our products? Share your experience on G2 and receive a $25 Amazon gift card for a limited time!

Tags
Grid
Asked by
Chris
Top achievements
Rank 1
Iron
Answers by
Vasko
Telerik team
Share this question
or