Camillia
asked on
SelectedItem in dropdownlist
I changed the code below to use SelectedItem. I wanted to remove the loop.
But my one line of code doesnt work. I see the value but when the page load, the ddl still has "<select item>".
what am I missing:
my code: ddlArea.SelectedItem.Value = area.Name;
Orig code below:
But my one line of code doesnt work. I see the value but when the page load, the ddl still has "<select item>".
what am I missing:
my code: ddlArea.SelectedItem.Value
Orig code below:
foreach (ListItem item in ddlArea.Items)
{
if (item.Text == area.Name)
{
ddlArea.SelectedValue = item.Value;
break;
}
}
you should be able to use without the loop, the other way is setting the selected items value i believe
try this
ddlArea.SelectedValue = area.Name;
try this
ddlArea.SelectedValue = area.Name;
SelectedValue is a property that specifies the value that is selected. Changing it selects the item with the new value. This property is used to select values only.
SelectedItem.Value is the value of the selected item. You use this property to change the value of the selected item. Changing it does not mean select an item with the new value.
So in brief:
SelectedValue is used to select an item with a specific value.
SelectedItem.Value is used to change the value of the selected item
SelectedItem.Value is the value of the selected item. You use this property to change the value of the selected item. Changing it does not mean select an item with the new value.
So in brief:
SelectedValue is used to select an item with a specific value.
SelectedItem.Value is used to change the value of the selected item
Looks like an unbounded winforms control. Is it?
In that case you should use just SelectedItem.
In that case you should use just SelectedItem.
ASKER
This is a web app, ASP.Net 2.0.
--The orig code works but i think that's not good for performance...looping..am I wrong?
--This doesnt work:
lArea.SelectedValue = area.Name;
I get : 'ddlArea' has a SelectedValue which is invalid because it does not exist in the list of items.
-- I put a debug step and I know ddlArea.SelectedItem.Value = area.Name; works because i see the value.
-- This is on a click event of a button but wondering of something happens in reload or page init to wipe it out. *** But if that's the case...howcome the orig code from ex-developers work??
--The orig code works but i think that's not good for performance...looping..am I wrong?
--This doesnt work:
lArea.SelectedValue = area.Name;
I get : 'ddlArea' has a SelectedValue which is invalid because it does not exist in the list of items.
-- I put a debug step and I know ddlArea.SelectedItem.Value
-- This is on a click event of a button but wondering of something happens in reload or page init to wipe it out. *** But if that's the case...howcome the orig code from ex-developers work??
i c, what is the actual values being stored as the item value , you compared the text of the item and used that items value, not the value of area.Name
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
burakiewicz: -- that's what the ex-developers coded and it doesnt make sense to me that much..that's why i want to remove the loop ...
item.value is the GUID number..for example.. ddlArea has name of US states so ddl ID is the GUID id and text , is for example..Texas..
item.value is the GUID number..for example.. ddlArea has name of US states so ddl ID is the GUID id and text , is for example..Texas..
ASKER
burakiewicz: -- just saw your second msg. Let me try that.
ok, then the line above should get what you need, if you knew the GUID you could use
ddlArea.SelectedValue = area.Guid or something like that
ddlArea.SelectedValue = area.Guid or something like that
ASKER
the first one worked, thanks.
ddlArea.SelectedValue = ddlArea.Items.FindByText(a rea.Name). Value;
ddlArea.SelectedValue = ddlArea.Items.FindByText(a
Have you tried using a counter variable, a regular for loop instead of foreach, and setting SelectedIndex?
Do you have anything to log the value of area.Name, such that you can prove this code ran?