requestItems method always empty

7 posts, 2 answers
  1. Jocelyn
    Jocelyn avatar
    148 posts
    Member since:
    Jul 2012

    Posted 20 Jun 2012 Link to this post

    Hi,

    My ComboBox looks like this:

    <telerik:RadComboBox ID="cboChambreDe" runat="server" MarkFirstMatch="True" EnableLoadOnDemand="True"
        HighlightTemplatedItems="True" Height="300px" LoadingMessage="Chargement" ItemsPerRequest="10"
        ShowMoreResultsBox="true" EnableVirtualScrolling="true" OnItemsRequested="cboChambre_ItemsRequested"
        DropDownWidth="300">
        
    </telerik:RadComboBox>

    When I click on a button, I call a javacript method that do:

    $find("cboChambreDe").requestItems("TOM1", false);

    It calls the ItemRequested on the server and it correctly find the item I want. But it doesn't refresh my ComboBox on the client, so it stays empty.

    What am I doing wrong?
  2. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 20 Jun 2012 Link to this post

    Hi Jocelyn,

    The items added in the ItemsRequested event persist only at the client-side. This is done for a better performance. These items will disappear after postback. Here postback on your button click is clearing the item.If you need the items to persist at the server, you need to add them outside the ItemsRequested event. 

    Thanks,
    Princy.
  3. Kalina
    Admin
    Kalina avatar
    918 posts

    Posted 25 Jun 2012 Link to this post

    Hi Jocelyn,

    Please make sure that you use cboChambreDe.ClientID when you try to obtain the RadComboBox client-side object
    <script type="text/javascript">
     
        function showItems() {
            var combo = $find("<%=cboChambreDe.ClientID %>");
            combo.requestItems("Tom1", false);
        }
     
    </script>
    <telerik:RadComboBox ID="cboChambreDe" runat="server"
        EnableLoadOnDemand="True"
        HighlightTemplatedItems="True" Height="300px"
        LoadingMessage="Chargement" ItemsPerRequest="10"
        ShowMoreResultsBox="true"
        EnableVirtualScrolling="true"
        OnItemsRequested="cboChambre_ItemsRequested"
        DropDownWidth="300">
    </telerik:RadComboBox>
     
    <input type="button" onclick="showItems()" value="click" />

    protected void cboChambre_ItemsRequested(object sender, RadComboBoxItemsRequestedEventArgs e)
    {
        RadComboBox combo = (RadComboBox)sender;
        combo.Items.Add(new RadComboBoxItem(e.Text));
    }

    Additionally you can check whether the request for items returns any data.

    All the best,
    Kalina
    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.
  4. Jocelyn
    Jocelyn avatar
    148 posts
    Member since:
    Jul 2012

    Posted 26 Jun 2012 Link to this post

    How can I select the item I just added after the requestItems?

    I tired to do:

    <script type="text/javascript">
      
        function showItems()
        {
            var combo = $find("<%=cboChambreDe.ClientID %>");
            combo.requestItems("Tom1", false);
            combo.get_items().getItem(0).select();
        }
      
    </script>

    But when select() is executed, my item is not added yet.

    Edit: And what is the proper way to call a resquesItem server side?

    For the moment, I am doing it like this:

    Dim e As RadComboBoxItemsRequestedEventArgs = New RadComboBoxItemsRequestedEventArgs()
    e.Text = ""
    cboChambreDe.Text = ""
    cboChambreDe_ItemsRequested(cboChambreDe, e)

    But I don't get the same behavior as if a user was entering a text in the combo box. Example, even though I set the e.Message, on client side, the message is still empty. Also, it correctly loads 10 items, but the requestResquested event is never fired until the user enter a new text in the combo box and I know that there is more than 10 items overall.

    Thanks.
  5. Answer
    Nencho
    Admin
    Nencho avatar
    1795 posts

    Posted 27 Jun 2012 Link to this post

    Hi Jocelyn,

    The invocation of requestItems method will result in a callback to the server which will then fire the ItemsRequested server side event. The appropriate event for selection of a specific item is the OnClientItemsRequested, which is fired immediately after the request for items is finished thus ensuring that the items are already loaded. Here is a sample implementation:
    function AddNewItem()
            {
                var combo = $find("<%= RadComboBox1.ClientID %>");
                combo.requestItems("Item1", true);
     
            }
     function onClientItemsRequested(sender, args)
            {
                sender.get_items().getItem(0).select();
            }

    As for the ItemsRequested event, it's not appropriate to manually invoke it from code behind. It's will be automatically fired when a request for items is being made.

    Kind regards,
    Nencho
    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.
  6. Jocelyn
    Jocelyn avatar
    148 posts
    Member since:
    Jul 2012

    Posted 27 Jun 2012 Link to this post

    Thanks Nencho!

    It works, but except for one scenario. If I call showItem() on a RadButton that have AutoPostBack set to true, the OnClientItemRequested is not called. Is there a way that I can achieve this or AutoPostBack must be set to false?

    Thanks.
  7. Answer
    Nencho
    Admin
    Nencho avatar
    1795 posts

    Posted 29 Jun 2012 Link to this post

    Hello Joceyn,

    Thank you for getting back to me.

    The items loaded on demand are not persisted after post-back thus populating the RadComboBox by clicking a button which performs a post back is not a good approach.

    Greetings,
    Nencho
    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.
Back to Top