# Event sequence

The API and event sequence and lifecycle of **RadGrid** are quite similar to **MS DataGrid/GridView**. The sequence of the events is as follows.

## RadGrid with EnableViewState set to true (default value)

### First page load

1. Page.Load
2. Grid_Instance.NeedDataSource
3. ItemCreated for each item
4. ItemDataBound for each item
5. Page.PreRender

### Normal postback from a control outside of RadGrid

1. ItemCreated for each item
2. Page.Load
3. Postback Events
4. Page.PreRender

### On server selection from Select/Deselect GridButtonColumn/Auto Postback on row click

1. ItemCreated for each item
2. Page.Load
3. ItemCommand
4. SelectedIndexChanged
5. Other postback events
6. Page.PreRender

### On edit/update/insert/delete action or paging/sorting/grouping/filtering operation

1. ItemCreated for each item
2. Page.Load
3. Grid_Instance.ItemCommand
4. Grid_Instance.EditCommand/UpdateCommand/InsertCommand or GridInstance.PageIndexChanged/SortCommand/GroupsChanging/ItemCommand
5. Grid_Instance.NeedDataSource
6. ItemCreated for each item
7. ItemDataBound for each item
8. Page.PreRender

### Calling Rebind()

Invoking the `Rebind()` method from a postback event handler of an outside control or RadGrid will raise the `NeedDataSource` event. Then grid items will be recreated so the `ItemCreated` and `ItemDataBound` events will be raised according to the cases above.

### With hierarchy

1. Page_Load
2. NeedDataSource
3. DetailTableDataBind

After the `NeedDataSource` event, the `DetailTableDataBind` is raised for each detail item that will be bound. You can use the `e.IsFromDetailTable` flag in `NeedDataSource` to check where the call originates from, so you can fetch data only when necessary.

Which items are bound depends on the `HierarchyLoadMode` property. For example, if it is set to `Client`, all detail items will be bound initially on the server so they can be expanded on the client.

If you set the `RetainExpandStateOnRebind` property to `true` and the `HierarchyLoadMode` to `Client`, the `NeedDataSource` and `DetailTableDataBind` events will be raised first for the items that need to be expanded.  These events will come before `Page_Load`, whether you rebind the grid or not because the items need to be created so their `Expanded` property can be set to `true`.

1. NeedDataSource for expanded items
2. DetailTableDataBind for expanded items
3. Page_Load
4. NeedDataSource
5. DetailTableDataBind

## RadGrid with EnableViewState set to false

### First page load

1. Page.Load
2. Grid_Instance.NeedDataSource
3. ItemCreated for each Item
4. ItemDataBound for each Item
5. Page.PreRender

### Normal postback from a control outside of RadGrid

1. Page.Load
2. Grid_Instance.NeedDataSource
3. ItemCreated for each Item
4. ItemDataBound for each Item
5. Postback Events
6. Page.PreRender

### On server selection from Select/Deselect GridButtonColumn/Auto Postback on row click

1. Page.Load
2. Grid_Instance.NeedDataSource
3. ItemCreated for each Item
4. ItemDataBound for each Item
5. ItemCommand
6. SelectedIndexChanged
7. Other postback events
8. Page.PreRender

### On edit/update/insert/delete action or paging/sorting/grouping/filtering operation

1. ItemCreated for each Item
2. Page.Load
3. GridInstance.NeedDataSource
4. ItemCreated for each Item
5. ItemDataBound for each Item
6. GridInstance.ItemCommand
7. Grid_Instance.EditCommand/UpdateCommand/InsertCommand or GridInstance.PageIndexChanged/SortCommand/GroupsChanging/ItemCommand
8. ItemCreated for each Item
9. ItemDataBound for each Item
10. Page.PreRender

### Calling Rebind()

Invoking the `Rebind()` method from a postback event handler of an outside control or RadGrid will **not** raise the `NeedDataSource` event. Then grid items will be recreated so the `ItemCreated` and `ItemDataBound` events will be raised according to the cases above.

To see how to rebind the grid in this case, review the [Rebind Grid with EnableViewState = false](https://www.telerik.com/products/aspnet-ajax/documentation/controls/grid/performance/rebind-grid-with-enableviewstate---false) article.

## See Also

* [Telerik RadGrid lifecycle](https://www.telerik.com/products/aspnet-ajax/documentation/controls/grid/control-lifecycle/telerik-radgrid-lifecycle)
* [Differences Between ItemCreated and ItemDataBound](https://www.telerik.com/products/aspnet-ajax/documentation/controls/grid/control-lifecycle/differences-between-itemcreated-and-itemdatabound-)
* [Commands that invoke Rebind Implicitly](https://www.telerik.com/products/aspnet-ajax/documentation/controls/grid/control-lifecycle/commands-that-invoke-rebind-implicitly)
* [Command Reference](https://www.telerik.com/products/aspnet-ajax/documentation/controls/grid/control-lifecycle/command-reference-)
* [How to Fire Command Events](https://www.telerik.com/products/aspnet-ajax/documentation/controls/grid/control-lifecycle/how-to-fire-command-events)
