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

NeedDataSource not fired after Rebind

19 Answers 980 Views
Grid
This is a migrated thread and some comments may be shown as answers.
EmpowerIT
Top achievements
Rank 2
EmpowerIT asked on 15 Oct 2009, 02:20 AM
Hi,
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:

 
void Grid_ItemCommand(object source, GridCommandEventArgs e) 
    { 
        if (e.CommandName == "Toggle"
        {        
            #region Toggle the database record 
           
            /// Do some stuff, update some records 
          
            #endregion       
           Grid.Rebind(); 
        } 
 
 <telerik:RadGrid ID="Grid" runat="server" AutoGenerateColumns="false" 
                        Width="300px" AllowMultiRowSelection="true" AllowPaging="false"
                        <SelectedItemStyle CssClass="SelectedItem"></SelectedItemStyle> 
                        <MasterTableView DataKeyNames="EntityId"
                            <RowIndicatorColumn Visible="False"
                                <HeaderStyle Width="20px" /> 
                            </RowIndicatorColumn> 
                            <ExpandCollapseColumn Resizable="False" Visible="False"
                                <HeaderStyle Width="20px" /> 
                            </ExpandCollapseColumn> 
                            <Columns> 
                                <telerik:GridBoundColumn DataField="Data" HeaderText="Data" UniqueName="Data"
                                </telerik:GridBoundColumn> 
                                
                                 
                                <telerik:GridButtonColumn ButtonType="LinkButton" CommandName="Toggle" Text="Toggle"
                                </telerik:GridButtonColumn> 
                            </Columns> 
                            <EditFormSettings> 
                                <PopUpSettings ScrollBars="None" /> 
                            </EditFormSettings> 
                        </MasterTableView> 
                    </telerik:RadGrid> 

Thanks,

19 Answers, 1 is accepted

Sort by
0
EmpowerIT
Top achievements
Rank 2
answered on 15 Oct 2009, 04:58 AM
Ok, i spent some more time looking into it, and seem to have found a solution.
The following article,
http://www.telerik.com/help/aspnet-ajax/grdrebindgridoncommandwithdatasourcepersistencemodenopersistence.html
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?

Thanks,



0
Veli
Telerik team
answered on 20 Oct 2009, 10:16 AM
Hi EmpowerIT,

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,
Veli
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
EmpowerIT
Top achievements
Rank 2
answered on 21 Oct 2009, 12:39 AM
Hi Veli,

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

Thanks again
0
Veli
Telerik team
answered on 23 Oct 2009, 12:30 PM
Hi EmpowerIT,

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.

Regards,
Veli
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
ehsan
Top achievements
Rank 1
answered on 13 Sep 2011, 11:45 AM
hi,
I faced same problem and spend a lot of time for it. EmpowerIt solution worked for me.
tanks
0
Contra
Top achievements
Rank 1
answered on 29 Sep 2011, 01:38 PM
Facing the same problem here.
Observations:
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)
0
Veli
Telerik team
answered on 30 Sep 2011, 08:48 AM
Hi Contra,

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.

Veli
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
0
Contra
Top achievements
Rank 1
answered on 30 Sep 2011, 09:18 AM
I was a little unclear, let me rephrase what happens:

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.
0
Veli
Telerik team
answered on 03 Oct 2011, 12:02 PM
I think I understand the scenario. I was referring to the same case in my previous post. Thank you for clarifying, though. Bottom line is, we need to identify why NeedDataSource fires in the Init phase of your control. Usually, RadGrid does not bind so early in the page life cycle. There are 2 potential reasons for that that we can suggest without knowing any further details:

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.

Veli
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
0
Elliott
Top achievements
Rank 2
answered on 05 Sep 2013, 07:44 PM
It's been almost 2 years since the last post to this thread, but the problem seems to remain.

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?

0
Radoslav
Telerik team
answered on 10 Sep 2013, 07:44 AM
Hi Elliott,

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:
http://www.telerik.com/help/aspnet-ajax/grid-rebind-grid-on-command-with-datasource-persistence-mode-no-persistence.html

Looking forward for your reply.

Regards,
Radoslav
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
0
Sam
Top achievements
Rank 1
answered on 14 Nov 2014, 08:47 PM
For what it's worth, this is still an issue. I'll give you my scenario if it helps you guys at all. I have a simple RadGrid. On Item Command, I have a custom command to update the entire grid. There are multiple pages on my grid so the function that does the update sets the AllowPaging = false, then it gets all the grid's items and performs the updates. The last step is to turn paging back on and then rebind. It shows with the same values as before. When I set the Datasource = nothing and then do the rebind, it works perfectly. There is nothing special about the page and viewstate is never turned off. So it definitely is not refreshing from the datasource until I set it to null first. 
0
Radoslav
Telerik team
answered on 19 Nov 2014, 08:20 AM
Hello Sam,

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

Regards,
Radoslav
Telerik
 

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.

 
0
Sam
Top achievements
Rank 1
answered on 19 Nov 2014, 12:05 PM
Sorry, I don't think I read your last post carefully enough. Understood. 
0
Stacy
Top achievements
Rank 1
answered on 16 Apr 2015, 02:25 PM
I have a grid that loads data when I hit a search button.  If I click another button that updates the database, and upon return call the "search" method to get the updated value, my grid.rebind() doesn't fire, even if I set grid.datasource = null beforehand.  This issue having been going on for years in this thread means telerik should prob look at their code and make rebind work as it should.  It should fire the needdatasource event without trying to determine if anything changed, it should just fire.
0
Stacy
Top achievements
Rank 1
answered on 16 Apr 2015, 02:38 PM
and further to my comment, after I update the database, my grid's datasource is already null so the event should fire anyway
0
Sam
Top achievements
Rank 1
answered on 16 Apr 2015, 02:45 PM
Stacy, I agree. I think the rebind method being called explicitly should force a grid refresh no matter the circumstances. However, when I had the issue, setting the datasource to null did the trick. Maybe you can post the code? 
0
Stacy
Top achievements
Rank 1
answered on 20 Apr 2015, 11:51 AM
I got around it by calling the need_datasource directly, and other events I needed.  But I'm sure i'll no doubt run into this again as this thread started in 2009 and it's still an ongoing issue...
0
Nathan
Top achievements
Rank 1
answered on 23 Dec 2016, 02:31 AM

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
        rigPostImages.Rebind()

Without the EnableViewstate and the Datasource it will not fire on the Rebind...

Thanks for posting the work around...

Tags
Grid
Asked by
EmpowerIT
Top achievements
Rank 2
Answers by
EmpowerIT
Top achievements
Rank 2
Veli
Telerik team
ehsan
Top achievements
Rank 1
Contra
Top achievements
Rank 1
Elliott
Top achievements
Rank 2
Radoslav
Telerik team
Sam
Top achievements
Rank 1
Stacy
Top achievements
Rank 1
Nathan
Top achievements
Rank 1
Share this question
or