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

[Solved] ComboBox and SelectedValue

5 Answers 241 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Wendy Hunt
Top achievements
Rank 2
Wendy Hunt asked on 07 Jan 2010, 06:17 PM
Hi -

My goal is to have a selection of one ComboBox populate a Selection in another ComboBox. 
Example... Box 1 selection is Food, Box 2 selection would then have a collection of Food Items like cheese, bread or soda. 

Here is my code:

        RadPanelItem ProjectSearchItem = (RadPanelItem)RadPanelBar1.FindItemByValue("ProjectSearch");  
 
        RadComboBox projectGroupRadComboBox = (RadComboBox)ProjectSearchItem.FindControl("projectGroupRadComboBox");  
        IQueryable allProjectGroup = ProjectGroup.GetAllBindings();  
        projectGroupRadComboBox.DataSource = allProjectGroup;  
        projectGroupRadComboBox.DataValueField = "ID";  
        projectGroupRadComboBox.DataTextField = "Name";  
        projectGroupRadComboBox.DataBind();  
        projectGroupRadComboBox.Filter = RadComboBoxFilter.Contains;  
 
        if (projectGroupRadComboBox.SelectedValue != null && projectGroupRadComboBox.SelectedValue.Length > 0)  
        {  
          projectGroup = ProjectGroup.Get(Convert.ToInt32(projectGroupRadComboBox.SelectedValue));  
          RadComboBox projectNameRadComboBox = (RadComboBox)ProjectSearchItem.FindControl("projectNameRadComboBox");  
          IQueryable allProjectName = Project.GetAllBindings(projectGroup.DataModel.ID);  
          projectNameRadComboBox.DataSource = allProjectName;  
          projectNameRadComboBox.DataValueField = "ID";  
          projectNameRadComboBox.DataTextField = "Name";  
          projectNameRadComboBox.DataBind();  
          projectNameRadComboBox.Filter = RadComboBoxFilter.Contains;  
        }  
        else 
        {  
          RadComboBox projectNameRadComboBox = (RadComboBox)ProjectSearchItem.FindControl("projectNameRadComboBox");  
          IQueryable allProjectName = Project.GetAllBindings(null);  
          projectNameRadComboBox.DataSource = allProjectName;  
          projectNameRadComboBox.DataValueField = "ID";  
          projectNameRadComboBox.DataTextField = "Name";  
          projectNameRadComboBox.DataBind();  
          projectNameRadComboBox.Filter = RadComboBoxFilter.Contains;  
        } 

The if statement and next line contain my problem.

I am used to using .SelectedValue with DDLs to select the Value, but with ComboBoxes it is not working.  What do I use in place of .SelectedValue, or is it more complicated than that?  I have read other posts pertaining to this and don't know which direction to take.

Thanks!!
wen

5 Answers, 1 is accepted

Sort by
0
Schlurk
Top achievements
Rank 2
answered on 07 Jan 2010, 10:05 PM
You could always look into the following demo which displays related ComboBoxes:

http://demos.telerik.com/aspnet-ajax/combobox/examples/functionality/multiplecomboboxes/defaultcs.aspx

As for the SelectedValue - are you actually looking to extract the contents of the Value property of the ComboBoxItem, or are you looking for the Text? Also, what exactly is the issue you are running into (errors etc.)?
0
Wendy Hunt
Top achievements
Rank 2
answered on 11 Jan 2010, 07:51 PM
The reason my SelectedValue kept coming up empty was because I was writing overtop of it.  I changed it and put half in a !ispostback and getting some of the results I need.  Here is my current code:

      private void PopulateProjectGroup()  
      {  
        RadPanelItem ProjectSearchItem = (RadPanelItem)RadPanelBar1.FindItemByValue("ProjectSearch");  
 
        RadComboBox projectGroupRadComboBox = (RadComboBox)ProjectSearchItem.FindControl("projectGroupRadComboBox");  
        IQueryable allProjectGroup = ProjectGroup.GetAllBindings();  
        projectGroupRadComboBox.DataSource = allProjectGroup;  
        projectGroupRadComboBox.DataValueField = "ID";  
        projectGroupRadComboBox.DataTextField = "Name";  
        projectGroupRadComboBox.DataBind();  
        projectGroupRadComboBox.Filter = RadComboBoxFilter.Contains;  
          
      }  
 
      private void PopulateProject()  
      {  
        RadPanelItem ProjectSearchItem = (RadPanelItem)RadPanelBar1.FindItemByValue("ProjectSearch");  
        RadComboBox projectGroupRadComboBox = (RadComboBox)ProjectSearchItem.FindControl("projectGroupRadComboBox");  
        RadComboBox projectNameRadComboBox = (RadComboBox)ProjectSearchItem.FindControl("projectNameRadComboBox");  
        if (projectGroupRadComboBox.SelectedValue != null && projectGroupRadComboBox.SelectedValue.Length > 0)  
        {  
          projectGroup = ProjectGroup.Get(Convert.ToInt32(projectGroupRadComboBox.SelectedValue));  
          IQueryable allProjectName = Project.GetAllBindings(projectGroup.DataModel.ID);  
          projectNameRadComboBox.DataSource = allProjectName;  
          projectNameRadComboBox.DataValueField = "ID";  
          projectNameRadComboBox.DataTextField = "Name";  
          projectNameRadComboBox.DataBind();  
          projectNameRadComboBox.Filter = RadComboBoxFilter.Contains;  
        }  
        else 
        {  
          IQueryable allProjectName = Project.GetAllBindings(null);  
          projectNameRadComboBox.DataSource = allProjectName;  
          projectNameRadComboBox.DataValueField = "ID";  
          projectNameRadComboBox.DataTextField = "Name";  
          projectNameRadComboBox.DataBind();  
          projectNameRadComboBox.Filter = RadComboBoxFilter.Contains;  
        }  
      }  
      protected void Page_Load(object sender, EventArgs e)  
      {  
        if (!Page.IsPostBack)  
        {  
          PopulateProjectGroup();  
        }  
        PopulateProject();  
      }  
 

Now my issue is with getting the ComboBox to Clear and show the results from being populated. 

It also currently shows the results from previous call and not the current call, in the combobox "window" (don't know what else to call it).  I have to click on the combobox to see the results instead of them just showing up in the window.  Any ideas?  I tried ClearSelection() prior to the PopulateProject if-statement without the results I'm looking for.  If I select a result from ProjectGroup in the ComboBox, the dropdown list in the combobox is refreshed but what appears in the window of the box is not refreshed. 

I'll try and work through the example you mentioned.  Thanks for directing me to it!  The examples really are good. 

Thanks!
wen
0
Simon
Telerik team
answered on 13 Jan 2010, 02:35 PM
Hi Wendy Hunt,

Your code looks OK. Can you describe in more detail what exactly is the issue and how do you reproduce it?

All the best,
Simon
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Wendy Hunt
Top achievements
Rank 2
answered on 13 Jan 2010, 10:14 PM
Hi Simon -

I really don't know how to describe it more but I'll give it a go.  When I select a Project Group, the ComboBox "window" of Project does not get populated, but if I hit the arrow of the box I can see the results in the drop down.  If I then decide that I don't like my selection and select a different Project Group, the previous selection stays in the window portion of the Project box, but the new results are in the drop down of the Project box.  I want the new results from my Project Group selection to be displayed in the window of the Project box and to clear out the previous selection results (and show the first selection of the new result list).

Let me also add, that all of this this is happening on a page where the functions are happening within a RadPanelBar.  I have another page that has no RadPanelBar that I can't even get to populate Project.  I'm using the same type of code, my only difference is that there is no FindControl line (RadComboBox projectGroupRadComboBox = (RadComboBox)ProjectSearchItem.FindControl("projectGroupRadComboBox");) for either ComboBox control, because they are not located within a PanelBar.  But one page at a time.  I first want to get the PanelBar page to work and then I'll tackle the non-PanelBar page.

How can I better describe my issues?  Any advice? 

Thanks! 
wen
0
Simon
Telerik team
answered on 26 Jan 2010, 12:01 PM
Hello Wendy Hunt,

I think the best way to describe this case is to prepare a simple working example and send it to us via a formal support ticket. In this way, we will be able to directly see the issue, investigate it and eventually provide a fix.

All the best,
Simon
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Tags
ComboBox
Asked by
Wendy Hunt
Top achievements
Rank 2
Answers by
Schlurk
Top achievements
Rank 2
Wendy Hunt
Top achievements
Rank 2
Simon
Telerik team
Share this question
or