RadGrid Model Binding SelectMethod fires twice

11 posts, 0 answers
  1. Eric
    Eric avatar
    16 posts
    Member since:
    Feb 2013

    Posted 09 Dec 2014 Link to this post

    Hello,

    I've been playing around with the ModelBinding features of ASP.Net Web Forms recently, and found it very nice. With an asp.net GridView, I am able to get my data source by setting a SelectMethod and returning my results. I use an asp.net DropDownList to filter the GridView's records.

    With an asp.net GridView and DropDownList, the following occurs:

    <asp:DropDownList ID="ddlActive" runat="server" SelectMethod="GetActiveFilters" AutoPostBack="true"
            AppendDataBoundItems="true" DataTextField="Text" DataValueField="Value">
            <asp:ListItem Text="All" Value="" />
        </asp:DropDownList>
     
        <br />
     
        <asp:GridView ID="Accounts" runat="server"
            ItemType="Accounts"
            SelectMethod="GetAccounts"
            AutoGenerateColumns="true">
     
        </asp:GridView>

    And In my code behind:

    Public Function GetAccounts(maximumRows As Integer?, startRowIndex As Integer?, <System.Runtime.InteropServices.Out()> ByRef totalRowCount As Integer, sortByExpression As String, <System.Web.ModelBinding.Control("ddlActive")> active As Boolean?) As IList(Of Account)
                    'Get accounts
                Dim list = DataSources.GetAccounts(active)
     
                totalRowCount = list.Count
     
                Return list
            
        End Function
     
        Public Function GetActiveFilters() As IEnumerable
     
            Dim l = {New With {.Text = "Active", .Value = True},
                     New With {.Text = "Inactive", .Value = False}}
     
            Return l
        End Function

    Now everything works fine. The SelectMethod fires only once, and when I select an item from the DropDown, the form posts back and the select method is fired again with the active parameter filled with content from the control "ddlActive".

    This is what I expected to happen.

    However, when all I do is simply change the asp:GridView to telerik:RadGrid, I get completely different behavior... undesirable behavior.

    The SelectMethod is fired twice, and when I select an item from the dropdown, the page posts back, but the select method is not called at all and my grid is not filtered. Am I missing additional configuration that the RadGrid needs?

    I am using version 2014.3.1024.45, Visual Studio 2013 Update 4

    Any ideas what I might be missing here? Why would the select method fire twice? Thanks.
  2. Eric
    Eric avatar
    16 posts
    Member since:
    Feb 2013

    Posted 11 Dec 2014 in reply to Eric Link to this post

    Anyone have any ideas on this?
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Konstantin Dikov
    Admin
    Konstantin Dikov avatar
    1793 posts

    Posted 12 Dec 2014 Link to this post

    Hi Eric,

    Indeed, on the initial page load, the SelectMethod will be called two times. I will have to forward this to our developers team, so they could further investigate it. Having in mind that this is observed only on the initial page load I could only assume that it is due to the fact that OnDataBinding event fires only on the initial load.

    For the time being, you can use the following simple workaround:
    <telerik:RadGrid ID="RadGrid1" GridLines="None" runat="server" AllowPaging="true" SelectMethod="GetProducts">
    </telerik:RadGrid>

    And the code-behind:
    public partial class Default : System.Web.UI.Page
    {
        bool loadData = false;
     
        public IQueryable<TestCustomObject> GetProducts()
        {
            if (IsPostBack) loadData = true;
     
            if (loadData)
            {
                List<TestCustomObject> data = new List<TestCustomObject>();
                for (int i = 0; i < 40; i++)
                {
                    data.Add(new TestCustomObject(1, 5));
                }
                return data.AsQueryable();
            }
     
            loadData = true;
            return null;
        }
    }
     
    public class TestCustomObject
    {
        public int ID { get; set; }
        public int Amount { get; set; }
        public TestCustomObject(int id, int amount)
        {
            this.ID = id;
            this.Amount = amount;
        }  
    }

    Please excuse us for any inconvenience caused by this.


    Regards,
    Konstantin Dikov
    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.

     
  5. Eric
    Eric avatar
    16 posts
    Member since:
    Feb 2013

    Posted 12 Dec 2014 in reply to Konstantin Dikov Link to this post

    Your workaround does prevent the the data from being loaded twice, however, selecting an item from the drop down still does not cause the SelectMethod to fire in the same way that it does for a GridView. When I select an item from the drop down, the grid's SelectMethod should fire again and the parameter should be populated with the drop down's value. With a GridView this happens correctly, with a RadGrid, it does not.

    Thanks
  6. Konstantin Dikov
    Admin
    Konstantin Dikov avatar
    1793 posts

    Posted 12 Dec 2014 Link to this post

    Hi Eric,

    Sorry for missing the part with the DropDown. 

    For optimization purposes, RadGrid will not call its SelectMethod on each postback, but it will call that method only on complex operations that requires rebind. 

    In your scenario, in order to force the grid to call its SelectMethod you could handle the server-side OnSelectedIndexChanged event of the DropDown and rebind the grid:
    protected void ddlActive_SelectedIndexChanged(object sender, EventArgs e)
    {
        RadGrid1.Rebind();
    }

    I have noticed that after calling the Rebind method, the SelectMethod is again called two times, so you may need to modify the logic there and use some global variables for handling this issue.


    Best Regards,
    Konstantin Dikov
    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.

     
  7. Eric
    Eric avatar
    16 posts
    Member since:
    Feb 2013

    Posted 12 Dec 2014 in reply to Konstantin Dikov Link to this post

    I see. I was hoping that by using the Model Binding features, I would not have to handle selection change events in this manner. Especially since the GridView doesn't require an explicit rebind.

    I noticed a few things with how the Model Binding works in asp.net. The select method only fires on the GridView IF the control doing the post back is referenced as a parameter in the select method.

    <System.Web.ModelBinding.Control("ddlActive")> active As Boolean?

    With this decorated parameter, GridView's SelectMethod will be called on postback only called IF the value of the dropdown has changed. Without that parameter, or if the value has not changed, the SelectMethod is not called. If the RadGrid behaved the same way, to me, that would prevent performance issues.

    Perhaps this should be considered a bug and updated to behave more consistently with the asp.net model binding features? 
  8. Konstantin Dikov
    Admin
    Konstantin Dikov avatar
    1793 posts

    Posted 17 Dec 2014 Link to this post

    Hi Eric,

    I completely agree with you that the calling of the SelectMethod twice should be considered as a bug and that is why I am logging this in our system, so our developers could further investigate it and hopefully, provide a fix in one of our future releases.

    As for the difference in the behavior between GridView and RadGrid, as I have explained in my previous post, RadGrid will request the data only when it is needed and this is done for optimization purposes only.


    Regards,
    Konstantin Dikov
    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.

     
  9. Christian
    Christian avatar
    11 posts
    Member since:
    Apr 2015

    Posted 27 May 2015 in reply to Konstantin Dikov Link to this post

    Is there any new information about the issue regarding that the Select method is fired twice (the first time with null values for the parameters)? In the current release the behavior is the same.

    It's not a problem if you have a datasource supporting IQueryable cause than there is only one real data access, but when your data tier only gives you data via the IList Interface for example, then it would be nice to only load the data once without a quirky workaround.

    Thanks in advance!

    Chris

     

     

  10. Konstantin Dikov
    Admin
    Konstantin Dikov avatar
    1793 posts

    Posted 01 Jun 2015 Link to this post

    Hi Chris,

    As you have correctly observed, the SelectMethod is still firing two times on the initial page load with our latest version and at the moment this seems to be a limitation of the control and its current implementation. 

    Until a fix is found, the only possible solution for handling the issue is with the workaround from my initial post.

    Please excuse us for any inconvenience caused by this.


    Regards,
    Konstantin Dikov
    Telerik
    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 Feedback Portal and vote to affect the priority of the items
  11. Christian
    Christian avatar
    11 posts
    Member since:
    Apr 2015

    Posted 20 Oct 2015 Link to this post

    The Problem still persists. Any news about planned fixing available?

    Thanks
    Chris

  12. Konstantin Dikov
    Admin
    Konstantin Dikov avatar
    1793 posts

    Posted 22 Oct 2015 Link to this post

    Hello Chris,

    As I have mentioned in my last post, this is a limitation with the current implementation of RadGrid and at this point it seems unlikely that a fix will be found for that particular problem. The main problem with the SelectMethod is that it fires once for the RadGrid control and once for the MasterTableView (due to the internal logic of the control).

    The only thing that I could suggest is that you create a public item in our Ideas & Feedback Portal and if the item receives enough votes, our developers team will investigate the scenario in greater details to determine if such fix is even possible in the context of RadGrid:

    Kind Regards,
    Konstantin Dikov
    Telerik
    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 Feedback Portal and vote to affect the priority of the items
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017