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

Set Default value selected in combobox at page load

10 Answers 2239 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
bharat kumar
Top achievements
Rank 1
bharat kumar asked on 06 Aug 2010, 03:29 PM

Hi,
i am using radcombobox and one scenrio i want that combobox having value from other sources which is working but i want that value to be shown as selected when page load not the user has to click the dropdown to see the value.

how to set the default value selected in dropdown. please help.

 <telerik:RadComboBox ID="cboTaskPersonSelector" runat="server" Width="195px" Height="150px"
                                    style="z-index: 107; left: 470px; position: absolute; top: 45px;"
                                    EnableLoadOnDemand="True" ShowMoreResultsBox="true"
                                    EnableVirtualScrolling="true" OnItemsRequested="cboTaskPersonSelector_ItemsRequested"
                                    Skin="Web20" AllowCustomText="false" EnableTextSelection="true" IsCaseSensitive="false"
                                    LoadingMessage="Loading..." DropDownWidth="500px" EnableScreenBoundaryDetection="true"
                                    Font-Names="Tahoma" Font-Size="12px" Enabled="false"
                                    OnClientDropDownClosed="OnClientBlur" ShowDropDownOnTextboxClick="false"
                                    AutoPostBack="true"
                                    OnSelectedIndexChanged="cboTaskPersonSelector_SelectedIndexChanged" SelectedValue='<%# Bind("TaskPersonId") %>'
                                    >

                                </telerik:RadComboBox>

protected void cboTaskPersonSelector_ItemsRequested(object sender, RadComboBoxItemsRequestedEventArgs e)
    { 
            if (e.Text.Length > 0)
            {
                t = TaskInfoCollection.SearchPossibleTasksForAllotmentByTitleOrNumber(e.Text);

                for (int i = itemOffset; i < endOffset; i++)
                {
                    ctl.Items.Add(new RadComboBoxItem(t[i].Number.ToString(), t[i].Id.ToString()));
                }
               
            }
            else if (Request.QueryString["popup"] != null)
            {
                               
                t = TaskInfoCollection.SearchPossibleTasksForAllotmentByTitleOrNumber(Request.QueryString["taskno"]);

                     for (int i = itemOffset; i < endOffset; i++)
                {
                    ctl.Items.Add(new RadComboBoxItem(t[i].Number.ToString(), t[i].Id.ToString()));
                }              
               
            }
        }    
    }

here the value is fill into combo box by  ctl.Items.Add() but it is not showing as selected value.
so when page load it look likes the dropdown is empty while when user click on dropdown it is there.

please help.

10 Answers, 1 is accepted

Sort by
0
Cori
Top achievements
Rank 2
answered on 06 Aug 2010, 04:42 PM
You should set the selected value in your Page_Load event. Right now, your setting it the ItemsRequested event, which only gets called when you start typing in the combobox or click on the dropdown arrow, thus the reason it shows when the control gets focus.

I hope that helps.
0
Kalina
Telerik team
answered on 09 Aug 2010, 11:57 AM
Hi,

By design when RadComboBox with Load-On-Demand feature enabled loads - it is empty and it has no items. Load-On-Demand fires and populates the control with data when user types in the input area or clicks on the drop-down toggle image (if there are no items in the RadComboBox). 

That is why preselecting an item is a little tricky – let me suggest you databind the RadComboBox at PageLoad event and preselect an item like this:

<telerik:RadComboBox ID="RadComboBox1" runat="server"
    EmptyMessage="Select" EnableLoadOnDemand="true"
    OnItemsRequested="RadComboBox1_ItemsRequested">
</telerik:RadComboBox>

protected void Page_Load(object sender, EventArgs e)
{
 
    if (!IsPostBack)
    {
        DataTable dataTable = CreateDataSource();
 
        foreach (DataRow dataRow in dataTable.Rows)
        {
            RadComboBoxItem item = new RadComboBoxItem();
            item.Text = (string)dataRow["Name"];
            item.Value = dataRow["ID"].ToString();
            RadComboBox1.Items.Add(item);
            item.DataBind();
        }
        // preselect the second item
        RadComboBox1.SelectedValue = "2";
 
    }
 
    
}
protected void RadComboBox1_ItemsRequested
    (object sender, RadComboBoxItemsRequestedEventArgs e)
{
    DataTable dataTable = CreateDataSource();
 
    foreach (DataRow dataRow in dataTable.Rows)
    {
        RadComboBoxItem item = new RadComboBoxItem();
        item.Text = (string)dataRow["Name"];
        item.Value = dataRow["ID"].ToString();
        RadComboBox1.Items.Add(item);
        item.DataBind();
    }
}
 
protected DataTable CreateDataSource()
{
    DataTable dataTable = new DataTable();
    dataTable.Columns.Add(new DataColumn("ID", typeof(string)));
    dataTable.Columns.Add(new DataColumn("Name", typeof(string)));
 
    DataRow dr = dataTable.NewRow();
    dr["ID"] = "1";
    dr["Name"] = "A";
    dataTable.Rows.Add(dr);
 
    DataRow dr2 = dataTable.NewRow();
    dr2["ID"] = "2";
    dr2["Name"] = "B";
    dataTable.Rows.Add(dr2);
 
    DataRow dr3 = dataTable.NewRow();
    dr3["ID"] = "3";
    dr3["Name"] = "C";
    dataTable.Rows.Add(dr3);
 
    return dataTable;
 
}

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
TechSavvySam
Top achievements
Rank 1
answered on 02 Nov 2011, 10:12 PM
Just checking to see if in the last 14 months you guys have come up with a better solution to this.

Thanks,
Sam
0
TechSavvySam
Top achievements
Rank 1
answered on 03 Nov 2011, 02:35 PM
Your solution pretty much defeats one of the major reasons we would be using a load on demand ComboBox.  Let's say instead of having 3 items in the drop down we have 5000 and on data entry users do a search to narrow their selection.

Your solution would bind all 5000 possible items to the combo box on an edit, totally defeating this major advantage of a a load-on-demand combobox.

I have what I think is a better solution that has 1 slight flaw with it (that probably could be fixed by Telerik if they looked into it):

Instead of binding the whole list when going into edit mode, do this instead:


RadComboBox1.Text = "B"
RadComboBox1.Value = "2"


The one flaw with this methodology is that to get the entire dropdown list again you have to clear the text in the combobox. But once you clear the text, you can retrieve the whole list again.

0
Kalina
Telerik team
answered on 16 Nov 2011, 05:27 PM
Hello Sam,

Thank you for sharing your solution for the community.

Please excuse me if my post has not been descriptive enough.
The sample code posted below is just a sample illustration of the approach that you can use to set a pre-selected item in RadComboBox with Load On Demand.  
Indeed, when you implement a real application scenario - you will have to extend this sample.

Let me suggest you take a look at this online demo. There are two RadComboBox controls (“Freight” and “CustomerName”) with Load On Demand that are nested within a RadGrid.  Please note that the approach demonstrated there is similar to yours.

Additionally – you can configure the RadComboBox to retrieve items in portions using the VirtualScrolling, ShowMoreResultsBox and ItemsPerRequest properties.You can take a look at the second RadComboBox(“Server-Side”) in this online demo and use a similar configuration. Please note that you have to set the ItemsPerRequest property (in order to define the number of items retrieved for one request) and the Height property (to define the dropdown height).

Regarding the last issue that you describe:
If there is a selected item in RadComboBox and user opens the control dropdown – only the selected item will be listed. This behaviour is by design and is correct.  A new request for items will fire when user deletes the text in control input.

Best wishes,
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
0
Radu
Top achievements
Rank 1
answered on 08 Nov 2012, 09:17 AM
Hi,
      Related to this solution, please provide another one for the next situation:

           I need to complete the default value of a RadComboBox at Load_Page on server side.
This control is not accessible from the page directly (cannot be found with FindControl method).
It's position is random in page and also I don't know the parent control (it is random).

            All what I know is the name of the control and  page object.

            How can I take a reference on this control in order to put the correct value at page_load event ?
Is any workaround related to this ?

Kind regards,
Radu.
   
0
Cat Cheshire
Top achievements
Rank 1
answered on 12 Nov 2012, 03:31 PM
I am not sure that I understand the issue here, but I think that this MSDN article will help you:
http://msdn.microsoft.com/en-us/library/31hxzsdw.aspx
0
Radu
Top achievements
Rank 1
answered on 13 Nov 2012, 06:27 AM
Hi Cat,

    Actually, the link doesn't help me. 
The Controls collection is empty. FindControls returns nothing. 
    I need to find on the page *any* telerik control when I am using a page and a name of the control.
    
    Do you have another idea please ?

Kind regards,
Radu.
0
Radu
Top achievements
Rank 1
answered on 16 Nov 2012, 05:59 AM
Hey... somebody alive there ?
0
Cat Cheshire
Top achievements
Rank 1
answered on 19 Nov 2012, 11:31 AM
Hi Radu,

You can paste some code here.
It is strange that at PageLoad the controls collection is empty.
How exactly are you trying to find the control?
Tags
ComboBox
Asked by
bharat kumar
Top achievements
Rank 1
Answers by
Cori
Top achievements
Rank 2
Kalina
Telerik team
TechSavvySam
Top achievements
Rank 1
Radu
Top achievements
Rank 1
Cat Cheshire
Top achievements
Rank 1
Share this question
or