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

[Solved] SelectedValue From Code Behind

2 Answers 606 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Joe
Top achievements
Rank 1
Joe asked on 21 Jul 2009, 10:39 PM
Hi,

I am converting a regular asp:drodownlist to a rad ComboBox.  It is a year drop down that gets populated on a formula from the code behind.  However, I can't seem to get the SelectedValue from the ComboBox in the same way I can from the asp:dropdownlist.

Here is my code behind for populating the asp:dropdownlist:
private void BindYears() 
    { 
        DateTime firstyear = DateTime.Now.AddYears(-62); 
 
        for (int i = 0; i > -47; i--) 
        { 
            DateTime year = firstyear.AddYears(i); 
            ddlYear.Items.Add(new ListItem(year.Year.ToString())); 
        } 
 
        ddlYear.SelectedIndex = 0
    } 
I changed "ListItem" to "RadComboBoxItem" and the combobox populated correctly.  However, upon postback of the combobox value I got an error as if the 'Value' wasn't being read.

Here is the codebehind for pulling the values from the dropdowns: 
private int Month 
    { 
        get { return Convert.ToInt32(ddlMonth.SelectedValue); } 
        set { ddlMonth.SelectedValue = value.ToString(); } 
    } 
 
    private int Day 
    { 
        get { return Convert.ToInt32(ddlDay.SelectedValue); } 
        set { ddlDay.SelectedValue = value.ToString(); } 
    } 
 
    private int Year 
    { 
        get { return Convert.ToInt32(ddlYear.SelectedValue); } 
        set { ddlYear.SelectedValue = value.ToString(); } 
    } 
 
    public DateTime SelectedDate 
    { 
        get 
        { 
            try 
            { 
                DateTime selecteddate = Convert.ToDateTime(Month + "/" + Day + "/" + Year); 
                return selecteddate; 
            } 
            catch 
            { 
                throw new Exception("The date you selected is not valid"); 
            } 
        } 
        set 
        { 
            try 
            { 
                Month = value.Month; 
                Day = value.Day; 
                Year = value.Year; 
            } 
            catch (Exception ex) 
            { 
            } 
        } 
    } 

These worked fine for asp:dropdownlist and for statically configured radcomboboxes.  No on the dynamic "year" when using a radcombobox.

Stuck.  Any help would be appreciated.  Thanks.
joe

2 Answers, 1 is accepted

Sort by
0
Accepted
Simon
Telerik team
answered on 22 Jul 2009, 08:46 AM
Hi Joe,

You need to also specify the Value of each Item as RadComboBox does not automatically do this as the DropDownList control:

ddlYear.Items.Add(new RadComboBoxItem(year.Year.ToString(), year.Year.ToString())); 

Now, the SelectedValue property will return the correct value.

Sincerely yours,
Simon
the Telerik team

Instantly find answers to your questions on the newTelerik Support Portal.
Check out the tipsfor optimizing your support resource searches.
0
Joe
Top achievements
Rank 1
answered on 22 Jul 2009, 03:21 PM
Thats it!  Thanks Simon.

I noticed when using statically configured asp:dropdown that I could get away with only using "Text" but had to add "value" when switching to radcombobox.  Your solution in the code behind seems to follow that same logic.

Everything works great.  Thanks again.

joe.
Tags
ComboBox
Asked by
Joe
Top achievements
Rank 1
Answers by
Simon
Telerik team
Joe
Top achievements
Rank 1
Share this question
or