PDA

View Full Version : [OPEN] Default value in a Combo Box



Jurke
Oct 15, 2008, 6:25 AM
Hi i have a combobox which looks like the code example, I have 2 problems with it:
1. I cant get a default value to be set in the combo.
2. The user is able to enter a value from the keybord, for example "10", but when i try to fetch the selected value, the selected item is -1. "string strValue = cbTest.Item.[cbTest.SelectedIndex].Text;"






<ext:ComboBox Width="70" ID="cbTest" runat="server" />




for (int i = 1; i <= 40; i++)
{
Coolite.Ext.Web.ListItem liItem = new Coolite.Ext.Web.ListItem();
liItem.Value = i.ToString();
liItem.Text = i.ToString();
cbTest.Items.Add(liItem);
}


How do I solve this?

Best regards
Mikael Jürke

Vladimir
Oct 15, 2008, 8:25 AM
Hi Mikael,

The ComboBox class has SelectedValue property. Using this property you can set predefined value



ComboBox1.SelectedItem.Value = "10";


Also after postback/ajaxevent you can read selected value and text



string val = ComboBox1.SelectedItem.Value;
string txt= ComboBox1.SelectedItem.Text;

Jurke
Oct 15, 2008, 9:00 AM
Thanks,
But if a user types in a value, for example "6" without actually selecting the value, the combo box visually has the value 6 but the selected value is something else. Is there something you can do about this. I think it will be confusing for the users.

Best regards
Mikael Jürke

Vladimir
Oct 15, 2008, 3:42 PM
Hi Mikael,

yes, you are right, I recommending to set ReadOnly="true" for ComboBox while we solving this problem

Thanks