Dynamically Created RadComboBox

3 posts, 0 answers
  1. Zash
    Zash avatar
    40 posts
    Member since:
    Aug 2005

    Posted 11 Nov 2011 Link to this post

    Hi,

    I created radcombobox dynamically. I want to add EnableLoadOnDemand functionality from codebehind. I need a sample of code in asp.net having this functionality doing from codebehind.

    Thanks.
  2. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 13 Nov 2011 Link to this post

    Hello Zash,

    You can try the following code snippet.

    C#:
    protected void Page_Load(object sender, EventArgs e)
       {
        RadComboBox rdcmb = new RadComboBox();
        rdcmb.ID = "RadComboBox1";
        rdcmb.AutoPostBack = true;
        rdcmb.MarkFirstMatch = true;
        rdcmb.AutoCompleteSeparator = ",";
        rdcmb.EnableLoadOnDemand = true;
        rdcmb.EnableAutomaticLoadOnDemand = true;
        rdcmb.LoadingMessage = "Loading...";
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ToString());
        String s = "select FirstName,EmployeeID from Employees";
        con.Open();
        SqlDataAdapter dr = new SqlDataAdapter(s, con);
        DataTable dt = new DataTable();
        dr.Fill(dt);
        con.Close();
        rdcmb.DataSource = dt;
        rdcmb.DataTextField = "FirstName";
        rdcmb.DataValueField = "EmployeeID";
        rdcmb.DataBind();
        form1.Controls.Add(rdcmb);
       }

    Thanks,
    Princy.
  3. Ivana
    Admin
    Ivana avatar
    657 posts

    Posted 16 Nov 2011 Link to this post

    Hello Rukunuz,

    To see how RadComboBox is bound to various data sources, you could refer to the DataBinding section of the Combo's online documentation.

    To enable the load on demand functionality, you need to set the property LoadOnDemand to true, and subscribe to the OnItemsRequested server-side event, where the items will be populated when they are requested on demand (which happens when the user opens the drop-down list, or type in the input field), as in the code-block below:
    protected void Page_Load(object sender, EventArgs e)
    {
        RadComboBox combo = new RadComboBox();
        combo.ID = "RadComboBox1";
        combo.EnableLoadOnDemand = true;
        combo.ItemsRequested += new RadComboBoxItemsRequestedEventHandler(combo_ItemsRequested);
        form1.Controls.Add(combo);
    }
    protected void combo_ItemsRequested(object sender, RadComboBoxItemsRequestedEventArgs e)
    {
        RadComboBox combo = (RadComboBox)sender;
        for (int i = 0; i <= 3; i++)
        {
            combo.Items.Add(new RadComboBoxItem("item_" + i.ToString()));
        }
    }

    More about load on demand mechanism, could be found at the following help article: RadComboBox: LoadOnDeman.

    All the best,
    Ivana
    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