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

EnableAutomaticLoadOnDemand property

14 Answers 437 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Boris
Top achievements
Rank 1
Boris asked on 05 Jun 2010, 06:00 PM
Dear Sirs!

EnableAutomaticLoadOnDemand

 

property is new in Q1 2010. I use SP2 of it. I cannot find suitable example.
I need use it in 2 scenario (in both I have big amount of the data: ~2500-2600 items):
1. Standalone combo. It is loaded when Page_Load event
   1.1. Not initial item. The user searched needed item from whole data.
   1.2. There is initial item and it has to be shown in initial loading. After the user has to have the possibility to change this item to another one when seraching between all the items.
I get the message like "cannot find teh item. The index is out off  boundary" 

2. The combo box is in Edit form of RadGrid. The type of Editform is Template.
  2.1. The form is in inserting mode - also the user has to search needed item.
  2.2. The form is in editing mode. The combo box is set to needed item and the user has to have the possibility to change this item to another.
I get the message like "incorrect callback".
Where is correct place to load the data in the combo box?

Thank you very much in advance.
Boris

14 Answers, 1 is accepted

Sort by
0
Kalina
Telerik team
answered on 07 Jun 2010, 04:17 PM
Hello Boris,

At first let me explain how Load-On-Demand works.
By design when RadComboBox is loaded - it is empty and it has no items.
Load-On-Demand fires when the user types in the input area or clicks on the drop-down toggle image when there are no items in the RadComboBox.
Automatic-Load-On-Demand is the codeless version of Load-On-Demand and works the same way.

That is why when RadGrid enters “EditMode” the RadComboBox control with Automatic-Load-On-Demand enabled has no items and it is a tricky task to preselect an item in it.

However you can overcome this issue by handling the RadGrid.OnItemDatabound event and creating an initial RadComboBoxItem to be displayed at the control input. When you delete the text initially displayed at RadComboBox input - Automatic-Load-On-Demand will fire and will populate the control dropdown with items:
protected void OnItemDataBoundHandler(object sender, GridItemEventArgs e)
{
  
    if (e.Item.IsInEditMode)
    {
            GridEditableItem item = (GridEditableItem)e.Item;
            if (!(e.Item is IGridInsertItem))
            {
                RadComboBox combo = (RadComboBox)item.FindControl("RadComboBox1");
                RadComboBoxItem preselectedItem = new RadComboBoxItem();
                preselectedItem.Text = item["CategoryName"].Text;
                preselectedItem.Value = item["CategoryID"].Text;
                combo.Items.Insert(0, preselectedItem);
                combo.SelectedIndex = 0;
            }
            else
            {
                TextBox textBoxID = (TextBox)item.FindControl("ProductIDBox");
                textBoxID.Visible = false;
  
            }
    }
}

In order to perform Edit, Insert and Delete operations please handle the RadGrid OnUpdateCommandOnDeleteCommand and  OnInsertCommand server events and simply add the necessary query parameters :
protected void RadGrid1_UpdateCommand(object source,Telerik.Web.UI.GridCommandEventArgs e)
{
  
    GridEditableItem editedItem = e.Item as GridEditableItem;
    RadComboBox comboBox = (RadComboBox)editedItem.FindControl("RadComboBox1");
    SqlDataSource1.UpdateParameters.Add(new Parameter("CategoryID", DbType.Int32, comboBox.SelectedValue));
    SqlDataSource1.UpdateParameters.Add(new Parameter("UnitPrice", DbType.Double, (e.Item.FindControl("UnitPriceTextBox") as TextBox).Text));
    SqlDataSource1.UpdateParameters.Add(new Parameter("ProductName", DbType.String, (e.Item.FindControl("ProductNameBox") as TextBox).Text));
    SqlDataSource1.UpdateParameters.Add(new Parameter("ProductID", DbType.Int32, (e.Item.FindControl("ProductIDBox") as TextBox).Text));
    SqlDataSource1.Update();
     
}

Please find more details at the sample page attached.
Feel free to contact us if you have additional questions.

Best wishes,
Kalina
the Telerik team

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 Public Issue Tracking system and vote to affect the priority of the items.
0
Boris
Top achievements
Rank 1
answered on 07 Jun 2010, 04:38 PM
Hello Kalina,

Thank you very much for help, it is very useful and I will use in another page.

But now I have changed scenario in my project - I did it more simple. 
I  moved working with the combo box where I want to use EnableAutomaticLoadOnDemand  property in standalone page, So only FIRST case is remained with 2 possibilities.

1) when I not initialize combobox with some item when page loading and
2) when I load this page with some initial item and want to show it in combobox

I cannot combine both possibilities in one page!

Can you please help me and give me working example for BOTH situations?

Thank you very much in advance.

Boris
0
Kalina
Telerik team
answered on 10 Jun 2010, 11:59 AM
Hello Boris,

You can easily implement logic to pre-select an item at RadComboBox input upon some condition.
Please note that RadComboBox items loaded on demand (using classical Load-On-Demand or Automatic-Load-On-Demand) are not persisted on server.

However you can "preselect" an item simply by adding an "artificial" item at RadComboBox and selecting it like this:
<telerik:RadComboBox ID="RadComboBox1" runat="server"
    Height="200px" EnableAutomaticLoadOnDemand="true"
    DataTextField="CategoryName" DataValueField="CategoryID"
    DataSourceID="SqlDataSource1">
</telerik:RadComboBox>
 
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
    ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
    SelectCommand="SELECT [CategoryID], [CategoryName], [Description] FROM [Categories]">
</asp:SqlDataSource>

protected void Page_Load(object sender, EventArgs e)
{
  //if(... some condition...)
  //  {
        RadComboBoxItem preselectedItem = new RadComboBoxItem();
        preselectedItem.Text = "Beverages";
        preselectedItem.Value = "0";
        RadComboBox1.Items.Insert(0, preselectedItem);
        RadComboBox1.SelectedIndex = 0;
   // }
}

Then if you delete the text initially displayed at RadComboBox input - Automatic-Load-On-Demand will fire and will populate the control dropdown with items.

Feel free to contact us if you have additional questions.

Kind regards,
Kalina
the Telerik team

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 Public Issue Tracking system and vote to affect the priority of the items.
0
Boris
Top achievements
Rank 1
answered on 10 Jun 2010, 12:05 PM
Hello Kalina!

Thank you very much!!!
I did approximately the same, and this works perfectly!

Once again, thank you very much!

Boris
0
Roni Lev
Top achievements
Rank 1
answered on 09 Aug 2010, 12:30 PM

 

 

0
Roni Lev
Top achievements
Rank 1
answered on 09 Aug 2010, 12:37 PM

 

 

making this on page_load event - works. it sets initial  value on combo with LoadOnDemand
rcb is the radcombo i insert the initial value.

RadComboBoxItem
ps = new RadComboBoxItem();

 

ps.Text = txt;

ps.Value = val;

rcb.Items.Insert(0, ps);

rcb.SelectedIndex = 0;

making the same thing later on not working. I can see in the combo my text and value selected but its not showing on the combo text box.

0
Kalina
Telerik team
answered on 12 Aug 2010, 12:18 PM
Hi Roni Lev,

I am afraid that we will need more details in order to help you.
It is correct that you are not able to add and preselect items at RadComboBox when Load-On-Demand has been fired. What is the scenario that you are trying to implement?
Could you please provide us a simplified working page that reproduces the issue?

Greetings,
Kalina
the Telerik team
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 Public Issue Tracking system and vote to affect the priority of the items
0
chad
Top achievements
Rank 1
answered on 13 Aug 2010, 02:19 AM
Hi Guys

I think I have a similar sounding query and was wondering if you could help

I have two Radcomboboxes that work in relation to each other as a filter - when you select an item in combobox 1 it fires a postback and causes the items in combobox 2 to reload in relation to what was selected in the first box - which works fine

My query is the following - a user is able to capture a new form item, which adds another list item to the second combobox - when they click save - the page does a post back and the first combobox keeps its origionally selected value (which is correct) - but I want the second combobox to default to the newly captured user item (which has now been added to that list). The problem I'm having is that the combobox only populates when a user physically clicks on it - so there is no default item in the combo for me to pre-select.

I've been trying to autopopulate the second combobox on page load based on the item that has been selected in the first combobox but I'm struggling to get that right - I was hoping that if I could autopopulate the second combo then I could simply select the newly added item ID within that list.

Its worth mentioning that I'm populating both of these comboboxes using a webservice call that returns RadComboBoxItemData - please can you help me with this - I desperately need to default the second combobox to the list item that the user has just captured when the screen reloads.

Cheers
0
chad
Top achievements
Rank 1
answered on 18 Aug 2010, 12:34 AM
wow - over a week and no response?

dont worry about it - sorted it out
0
Kalina
Telerik team
answered on 18 Aug 2010, 03:25 PM
Hello chad,

Glad that you managed to find solution on your own!
Please note that the response time for forum posts is 72 hours and it does not include weekends.

Feel free to use our community forum if you need further assistance.

Best wishes,
Kalina
the Telerik team
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 Public Issue Tracking system and vote to affect the priority of the items
0
Guss
Top achievements
Rank 2
Veteran
answered on 15 Aug 2016, 04:24 PM

Isn't this a better technique?

The grid page:

<telerik:GridTemplateColumn DataField="Country" FilterControlAltText="Filter Country column" HeaderText="Country" EditFormHeaderTextFormat="<label>{0}:</label>" SortExpression="Country" UniqueName="Country" HeaderStyle-CssClass="hidden-sm" ItemStyle-CssClass="hidden-sm" FooterStyle-CssClass="hidden-sm">
  <ItemTemplate><%# Eval("Country") %></ItemTemplate>
  <EditItemTemplate>
    <rmr:Country runat="server" id="Country" SelectedValue='<%# Bind("CountryId") %>' SelectedText='<%# Eval("Country") %>' />
  </EditItemTemplate>
</telerik:GridTemplateColumn>

The the user control:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="Country.ascx.cs" Inherits="RMR.Web.Controls.Country" %>
<telerik:RadComboBox ID="rcbCountry" runat="server" DataSourceID="sdsCountries" DataValueField="LocationId" DataTextField="Location" EmptyMessage="start typing..." RenderMode="Lightweight" Width="186px" DropDownAutoWidth="Enabled" Height="100px" EnableAutomaticLoadOnDemand="true" showmoreresultsbox="true" enablevirtualscrolling="true" itemsperrequest="10" MarkFirstMatch="True"  MinFilterLength="1"></telerik:RadComboBox>
<asp:SqlDataSource ID="sdsCountries" runat="server" ConnectionString="<%$ ConnectionStrings:RMRDatabase %>" SelectCommand="SELECT l.LocationId, l.Location FROM rmr.Locations l WHERE l.[Level] = 3 ORDER BY l.Location" SelectCommandType="Text"></asp:SqlDataSource>

And the code behind:

using System;
using System.Linq;
using Telerik.Web.UI;
 
namespace RMR.Web.Controls
{
    public partial class Country : System.Web.UI.UserControl
 
    {
        public static string _text;
        public string SelectedText
        {
            get
            {
                return rcbCountry.SelectedItem.Text;
            }
            set
            {
                if (!String.IsNullOrEmpty(value))
                {
                    _text = value;
                }
            }
        }
        public string SelectedValue
        {
            get
            {
                return rcbCountry.SelectedValue;
            }
            set
            {
                if (!String.IsNullOrEmpty(value))
                {
                    RadComboBoxItem preselectedItem = new RadComboBoxItem();
                    preselectedItem.Text = _text;
                    preselectedItem.Value = value;
                    rcbCountry.Items.Insert(0, preselectedItem);
                    rcbCountry.SelectedIndex = 0;
                }
            }
        }
    }
}

0
Guss
Top achievements
Rank 2
Veteran
answered on 15 Aug 2016, 04:36 PM

With the above, you have a control that you can just drop in the edit item template, and you do not have to care if it is insert or edit form. You can also re-use the control outside the grid, as is, without anymore code.

Things you may ignore from my above example:

  • EditFormHeaderTextFormat="<label>{0}:</label>" is just to make sure the label of the control has the same style as dynamic generated column edit labels.
  • HeaderStyle-CssClass="hidden-sm" ItemStyle-CssClass="hidden-sm" FooterStyle-CssClass="hidden-sm" is just me hiding some columns in my bootstrap page if the users viewport very small.
  • If you do not want to pass the SelectedText, you can always make a roundabout to the server and fetch the text based on the value. (but that is a waist as the applicable id and text is already in my select statement of the grid, so just get it from there.
  • My select command for sdsCountries may look weird: I'm getting it out of a hierarchical table containing Region, Subregion, country, state / province, and Countries is on [Level]=3 in the database. Your select could be as simple as Select [CountryId], [CountryName] from....
  • In the control, do not atttempt to do things in OnPageLoad, as this will not work... it will be 1 * asp.net lifecycle behind.

All of above tested and does behave as expected.
My grid is inside an asp:UpdatePanel and the whole page live inside a masterpage.

 

0
Nencho
Telerik team
answered on 18 Aug 2016, 01:22 PM
Hello Guss,

Indeed the technique that you had demonstrated is valid and suitable for a large range of scenarios. I would like to thank you for taking a time to share your implementation and suggestion with the community!

Regards,
Nencho
Telerik by Progress
Do you need help with upgrading your ASP.NET AJAX, WPF or WinForms projects? Check the Telerik API Analyzer and share your thoughts.
0
Guss
Top achievements
Rank 2
Veteran
answered on 18 Aug 2016, 01:36 PM

My pleasure.
I'm a lazy "google-copy-past" coder, so I can't live without these forums :-)
So maybe not a real copy-paste'r, but surely appreciate the Telerik forum just to show me the direction, and sometimes you have to give back... I should make the effort and do it more.

Regards
Guss

Tags
ComboBox
Asked by
Boris
Top achievements
Rank 1
Answers by
Kalina
Telerik team
Boris
Top achievements
Rank 1
Roni Lev
Top achievements
Rank 1
chad
Top achievements
Rank 1
Guss
Top achievements
Rank 2
Veteran
Nencho
Telerik team
Share this question
or