Set Default value selected in combobox at page load

11 posts, 0 answers
  1. bharat kumar
    bharat kumar avatar
    53 posts
    Member since:
    Jan 2010

    Posted 06 Aug 2010 Link to this post


    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.
  2. Cori
    Cori avatar
    562 posts
    Member since:
    Jul 2010

    Posted 06 Aug 2010 Link to this post

    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.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Kalina
    Admin
    Kalina avatar
    918 posts

    Posted 09 Aug 2010 Link to this post

    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
  5. TechSavvySam
    TechSavvySam avatar
    38 posts
    Member since:
    Jul 2009

    Posted 02 Nov 2011 Link to this post

    Just checking to see if in the last 14 months you guys have come up with a better solution to this.

    Thanks,
    Sam
  6. TechSavvySam
    TechSavvySam avatar
    38 posts
    Member since:
    Jul 2009

    Posted 03 Nov 2011 Link to this post

    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.

  7. Kalina
    Admin
    Kalina avatar
    918 posts

    Posted 16 Nov 2011 Link to this post

    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
  8. Radu
    Radu avatar
    4 posts
    Member since:
    Nov 2012

    Posted 08 Nov 2012 Link to this post

    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.
       
  9. Cat Cheshire
    Cat Cheshire avatar
    128 posts
    Member since:
    Jun 2010

    Posted 12 Nov 2012 Link to this post

    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
  10. Radu
    Radu avatar
    4 posts
    Member since:
    Nov 2012

    Posted 13 Nov 2012 Link to this post

    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.
  11. Radu
    Radu avatar
    4 posts
    Member since:
    Nov 2012

    Posted 15 Nov 2012 Link to this post

    Hey... somebody alive there ?
  12. Cat Cheshire
    Cat Cheshire avatar
    128 posts
    Member since:
    Jun 2010

    Posted 19 Nov 2012 Link to this post

    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?
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017