+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Is it possible to programatically set the selected index of RadComboBox?

Is it possible to programatically set the selected index of RadComboBox?

4 posts, 1 answered
  1. andersleet
    andersleet avatar
    155 posts
    Registered:
    03 Jun 2009
    08 Jul 2009
    Link to this post
    I know it says it is read-only, is there a way to override it? I believe I can use the following code to select the item I want:

    itemType.SelectedItem = itemType.FindItemByValue(item.stateid.ToString()); 

    But I can't as it stands because SelectedItem is read-only.

    regards,
    anders
  2. Georgi
    Georgi avatar
    3583 posts
    Registered:
    20 Sep 2016
    09 Jul 2009
    Link to this post
    Hello andersleet,

    Please take a look at the RadComboBox manual article Working with Items in Server Side Code. Note the Selecting Items section and the sample codes:
     // Use RadComboBoxItem.Selected 
    RadComboBoxItem item = RadComboBox1.FindItemByText("Item 2"); 
    item.Selected = true
     
    // Use RadComboBox.SelectedIndex 
    int index = RadComboBox1.FindItemIndexByValue("2"); 
    RadComboBox1.SelectedIndex = index; 
    //You can also use the SelectedValue property: 
    //RadComboBox1.SelectedValue = value;  

    You can also do a Client Side item select.


    Kind regards,
    Georgi
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
    Answered
  3. andersleet
    andersleet avatar
    155 posts
    Registered:
    03 Jun 2009
    09 Jul 2009
    Link to this post
    Thanks for the code Georgi. I think the problem is because the ComboBox is databound, it doesn't know it is there. I verified this by trying to find a specific item that is added to the ComboBox when it is databound (result was null), then I added a static item and it had no problem finding that one.  I was able to solve a similar issue with a Repeater for news, if the author was empty I didn't want it to display the "Posted by..." text so I did this:

    protected void Page_Load(object sender, EventArgs e) 
        repeater.ItemDataBound +=new RepeaterItemEventHandler(repeater_ItemDataBound); 
    protected void repeater_ItemDataBound(object sender, RepeaterItemEventArgs e) 
        if (e.Item.DataItem != null
        { 
            Literal PostedBy = (Literal)e.Item.FindControl("litPostedBy"); 
            Literal PostedOn = (Literal)e.Item.FindControl("litPostedOn"); 
            string Author = (string)DataBinder.Eval(e.Item.DataItem, "Author"); 
     
            if (string.IsNullOrEmpty(Author)) 
            { 
                PostedBy.Text = "Posted "
                PostedOn.Text = ""
            } 
              
        } 

    So instead of "Posted by on July 9, 2009", I get "Posted July 9, 2009". Can somehow apply similar logic in this situation?

    Thanks!
    anders
  4. andersleet
    andersleet avatar
    155 posts
    Registered:
    03 Jun 2009
    09 Jul 2009
    Link to this post
    Actually, I just solved it myself:

    I changed the original code:

    protected void Page_Load(object sender, EventArgs e) 
        ItemManager = new Items(); 
        backButton.NavigateUrl = CreateHostViewCommand("ItemsListView"nullnull); 
        cancel.NavigateUrl = CreateHostViewCommand("ItemsListView"nullnull); 
     
        if (DataItemId != Guid.Empty) 
        { 
            LoadItem(); 
        }    

    to this:

    protected void Page_Load(object sender, EventArgs e) 
        ItemManager = new Items(); 
        backButton.NavigateUrl = CreateHostViewCommand("ItemsListView"nullnull); 
        cancel.NavigateUrl = CreateHostViewCommand("ItemsListView"nullnull); 
     
        stateDropDown.DataBound += new EventHandler(stateDropDown_DataBound); 
         
    void stateDropDown_DataBound(object sender, EventArgs e) 
        if (DataItemId != Guid.Empty) 
        { 
            chkUpdateContainer.Visible = true
            LoadItem(); 
        } 

    Now I can see all of my databound items and use the code you have provided :D

    Cheers!
    anders
Register for webinar
4 posts, 1 answered