This is a migrated thread and some comments may be shown as answers.

Sorting a RadGrid twice - order of events?

5 Answers 152 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Ando
Top achievements
Rank 1
Ando asked on 21 Sep 2010, 04:08 PM
On a page, we have a RadGrid. We're running into some problems that I am thinking might have to do with events firing, and in what order they fire. The error comes if you sort a column, then click to sort it again. After tracing down the code-behind, I think I see the problem, but I am curious if this is intended behavior, or if there's something we can do. The problem on our end stems from a particular variable in the code-behind. The variable is actually an object reference, and the error is a null reference exception thrown inside the Grid's ItemCreated event. The object is set in a  private method called in the Page_Load event.

I would post the code, but it is well over 1,000 lines long. So for simplicity's sake, here's the order of events I traced:

1) I enter the page -> Page_Load fires, populates the object reference and binds the RadGrid, also firing ItemCreated and using the reference with no problems
2) I click a column -> Page_Load fires again, populates the object reference again and apparently re-binds the RadGrid, firing the ItemCreated event and using the reference with no problems.
3) I click the same column to sort in the opposite direction -> ItemCreated fires and the object reference is null, crashing.

The private method call in Page_Load is NOT dependent on the page being a postback or not, - it is always called on Page_Load. So why does Page_Load fire on the first sort, but not on the second sort?

Thanks in advance for any help offered!

Ando Poore

5 Answers, 1 is accepted

Sort by
0
Marin
Telerik team
answered on 24 Sep 2010, 11:59 AM
Hi Ando,

The proper way to perform advanced databinding scenarios in the RadGrid when you need paging, sorting, filtering etc. would be to use the NeedDataSource event and set there the datasource of the control. Further information on how to do this is available here. The incorrect binding could also be the reason for the Page_Load event not firing as expected. When you click the column to sort, the grid tries to recreate its structure from the previously saved viewstate (which in the page life cycle happnes before Page_Load) and fires directly ItemCreated, which causes the problem in your case.
Please try using the NeedDataSource and see how this works.

Regards,
Marin
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Ando
Top achievements
Rank 1
answered on 24 Sep 2010, 09:17 PM
Thanks for the reply.

Taking your advice, I changed the coding so that inside the NeedDataSource event, I check that object reference I mentioned, and if it is Nothing, I populate it.

However, this did not resolve the issue. Upon more breakpoints and debugging, I found that the second sorting click also fired ItemCreated without hitting either Page_Load or NeedDataSource.

Any other help you can offer?

Thanks!

Ando
0
Marin
Telerik team
answered on 27 Sep 2010, 02:29 PM
Hi Ando,

You are right the second time it does not hit any of those events.
I see two options here. You can either initialize your object reference before the loading of the viewstate started (that is the OnInit event) or if it is possible it would be much better to use the OnItemDataBound event, which is also called for every item but after it has been databound and then you surely have your object reference set. The following article should prove useful in this regard.

Hope this helps.

Greetings,
Marin
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Ando
Top achievements
Rank 1
answered on 28 Sep 2010, 02:05 PM
Thanks again for the reply.

I moved my code to re-populate the object reference to the ItemDataBound event, but that still did not resolve the issue. The second sorting still hits the ItemCreated event first, and my page is still crashing.

Is there any event that will fire before ItemCreated on a second sort?
0
Marin
Telerik team
answered on 29 Sep 2010, 05:30 AM
Hi Ando,

Thanks for getting back to us.

You should not re-populate the object reference in the ItemDataBound event (this only needs to be done in the NeedDataSource event). You should use ItemDataBound instead of ItemCreated (i.e. you should move your code from ItemCreated to ItemDataBound, where the object reference is not null.)
I have attached a sample application showing this approach.

Hope this helps.

Sincerely yours,
Marin
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
Grid
Asked by
Ando
Top achievements
Rank 1
Answers by
Marin
Telerik team
Ando
Top achievements
Rank 1
Share this question
or