I seem to experiencing a problem where the NeedDataSource event is not being fired after Rebind is called. The grid shows the data on load etc, but after clicking on a link button in the grid, the NeedDataSource event is not firing (confirmed via debugger)
I am calling Rebind method from within the ItemCommand event. Please view the code below:
19 Answers, 1 is accepted
The following article,
suggested that we explicitly set the Grid.DataSource to null, before calling the Rebind method. This was based on having EnableViewState set to false on the grid.
However, it still does not explain why it was happening. My RadGrid has EnableViewState set to true! So the above should not have applied to it ... Can anyone please explain why that was happening?
The help article you refer to applies only to cases when you have RadGrid's ViewState turned off. Additionally, it does not discuss the firing of the NeedDataSource event when the Rebind() method is called. Rebind() should always fire the NeedDataSource event, not matter in what scenario.
If you have are sure your NeedDataSource event does not fire when you call Rebind(), please, consider opening a regular support ticket where you can upload a small runnable project we can test. Refer to this forum thread if you choose to do so.
All the best,
the Telerik team
Thanks for the response. I've investigated further, and it does seem to work correclty in some places, and not in others. Ill give you the entire scenario.
1. My rad grid control is part of a custom user control, lets call it the RadGridCustomControl. This rad grid has a link button column that fires off a command that is handled in the ItemCommand event. This item command makes changes to some data and then calls the radgrids Rebind method.
2. The "RadGridCustomControl" is then placed inside another custom control. It seems to work in some situations, but not in others.
The only difference between the flow of these custom controls that I have picked up on during debugging, is that in the scenario where it does NOT work, the need datasource even is fired BEFORE the itemcommand event. Then, when rebind is called in the itemcommand event, Need datasource is NOT called again.
The places where it does work i have noticed that the itemcommand event is fired first, then when rebind is called, the need datasource event is fired.
In both situations, the RadGridCustom control works correctly if i explicity set the grid's datasource to NULL before calling rebind. EnableViewState is set to true on the grid
From my limited understanding, it seems that the NeedDataSource event will not fire twice in the same page lifecycle .... But I am most probably wrong ;)
If anyone could shed any light on this, it would be much appreciated.
I don't want to lodge a support ticket just yet as I cannot submit the whole project, and it will require some time to create a small project that reproduces the problem
Let me share a few observations:
1. RadGrid's NeedDataSource event fires whenever RadGrid needs to rebind, and always after you call Rebind(). It may fire N number of times in a page lifecycle, not only once.
2. Custom item commands do not invoke automatic rebinding. The correct scenario for your case would be ItemCommand first, them NeedDataSource when you call Rebind().
3. If the rebinding problem gets fixed when you set DataSource = null, this probably indicates turned off ViewState and problems with that. Check your parent containers and/or content pages to make sure they all have their ViewState ON. For more info, refer to the following help article.
4. Check how your user controls are added into each other and on the page. Make sure they are all either added declaratively in the markup, or if you are loading the user controls programmatically, make sure you observe a few requirements as listed here.
the Telerik team
I faced same problem and spend a lot of time for it. EmpowerIt solution worked for me.
When adding a usercontrol containing the radgrid in the Page_init, the need_datasource will be triggered with the reason initialLoad. When explicitly rebinding inside in a postback the need_datasource will not trigger. Is this because the need_datasource just got triggered by the init? By setting datasource to null seems to retrigger the need_datasource, however (as stated above)
Is the container in which RadGrid is added to databound? RadGrid may rebind automatically when its parent container is rebound. Otherwise, the NeedDataSource should not be fired as early as Page_Init. If you can show us some sample code demonstrating your scenario, we may be able to find the cause of this issue.
the Telerik team
the radgrid is inside a usercontrol which is added in page_init to a placeholder in the .aspx.
on postback, the uc is added as usual (in init), page load inside the uc is run, the need_Data is run, so far so good.
Then the postback_method is run; inside the postback_method I do a .Rebind(), but the need_data is not run which will cause the wrong data to be displayed since I the postback_method altered the data.
1. ViewState is disabled for the control tree in which your RadGrid resides. In this scenario RadGrid needs to rebind in the Init phase, so that it has its controls structure recreated properly.
2. The container in which RadGrid is added is databound. In this case, RadGrid binds as a result of databinding command coming from a parent container.
Identifying why RadGrid binds so early in the page life cycle will help us determine what we can do to improve that. Ideally, (with ViewState enabled and RadGrid properly databinding), the grid would not databind on every postback, but only when required (e.g. page/sort/filter or any other databinding command) or as a result of an explicit call to Rebind(), which is what you are doing. In your current scenario RadGrid is rebinding twice - once on Init and once on call to Rebind. This may be excessive and we are trying to understand why.
I hope I am clearer.
the Telerik team
I had a similar issue to EmpowerIT: After updating the database outside of the grid, the rebind did not refresh the grid. However, explicitly setting the data source to 'Nothing' then rebinding worked!
Has this been resolved by the Telerik team?
Could you please post your aspx page with the related code behind file? Thus we will be able to gather more details about your scenario and provide you more to the point answer.
Also calling rebind operation of RadGrid does not mean that the Grid will fire its NeedDataSource event. RadGrid decides when it does need to fire the event. For performance reasons if you call simultaneously rebind () twice on server side only the first time the NeedDataSource event will be fired. This is designed in that way because the RadGrid track that it has already assigned data source and does not need to get it again. So in this case in order the grid to call its NeedDataSource method you need to remove its datasource by setting it to null.
Additionally if you disable the ViewState of the RadGrid or on some of its parent controls and rebind grids on some command you need to explicitly set its datasource to null as is described in the following article:
Looking forward for your reply.
Like I said in my previous post the RadGrid’s NeedDataSource event is fired when is needed. When the RadGrid has data and detects that it is not changed, the NeedDataSource will not be fired. This is not an issue – this behavior is by design. In order to force the grid to fire its NeedDataSource event you need to set its DataSource to null.
Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.
This is the end of 2016 and this is still an issue. To get the NeedDatasource to fire you have to still set the EnableVIewstate = False and
rigPostImages.DataSource = Nothing
Without the EnableViewstate and the Datasource it will not fire on the Rebind...
Thanks for posting the work around...