NeedDataSource not fired after Rebind

19 posts, 0 answers
  1. EmpowerIT
    EmpowerIT avatar
    117 posts
    Member since:
    Jun 2007

    Posted 14 Oct 2009 Link to this post

    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,
  2. EmpowerIT
    EmpowerIT avatar
    117 posts
    Member since:
    Jun 2007

    Posted 14 Oct 2009 Link to this post

    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,



  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Veli
    Admin
    Veli avatar
    2002 posts

    Posted 20 Oct 2009 Link to this post

    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.
  5. EmpowerIT
    EmpowerIT avatar
    117 posts
    Member since:
    Jun 2007

    Posted 20 Oct 2009 Link to this post

    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
  6. Veli
    Admin
    Veli avatar
    2002 posts

    Posted 23 Oct 2009 Link to this post

    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.
  7. ehsan
    ehsan avatar
    1 posts
    Member since:
    Nov 2010

    Posted 13 Sep 2011 Link to this post

    hi,
    I faced same problem and spend a lot of time for it. EmpowerIt solution worked for me.
    tanks
  8. Contra
    Contra avatar
    10 posts
    Member since:
    Aug 2009

    Posted 29 Sep 2011 Link to this post

    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)
  9. Veli
    Admin
    Veli avatar
    2002 posts

    Posted 30 Sep 2011 Link to this post

    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
  10. Contra
    Contra avatar
    10 posts
    Member since:
    Aug 2009

    Posted 30 Sep 2011 Link to this post

    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.
  11. Veli
    Admin
    Veli avatar
    2002 posts

    Posted 03 Oct 2011 Link to this post

    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
  12. Elliott
    Elliott avatar
    391 posts
    Member since:
    May 2010

    Posted 05 Sep 2013 Link to this post

    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?

  13. Radoslav
    Admin
    Radoslav avatar
    1564 posts

    Posted 10 Sep 2013 Link to this post

    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.
  14. Sam
    Sam avatar
    60 posts
    Member since:
    Mar 2011

    Posted 14 Nov 2014 in reply to Radoslav Link to this post

    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. 
  15. Radoslav
    Admin
    Radoslav avatar
    1564 posts

    Posted 19 Nov 2014 Link to this post

    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.

     
  16. Sam
    Sam avatar
    60 posts
    Member since:
    Mar 2011

    Posted 19 Nov 2014 in reply to Radoslav Link to this post

    Sorry, I don't think I read your last post carefully enough. Understood. 
  17. Stacy
    Stacy avatar
    98 posts
    Member since:
    Dec 2010

    Posted 16 Apr 2015 Link to this post

    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.
  18. Stacy
    Stacy avatar
    98 posts
    Member since:
    Dec 2010

    Posted 16 Apr 2015 in reply to Stacy Link to this post

    and further to my comment, after I update the database, my grid's datasource is already null so the event should fire anyway
  19. Sam
    Sam avatar
    60 posts
    Member since:
    Mar 2011

    Posted 16 Apr 2015 Link to this post

    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? 
  20. Stacy
    Stacy avatar
    98 posts
    Member since:
    Dec 2010

    Posted 20 Apr 2015 Link to this post

    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...
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017