Link to home
Start Free TrialLog in
Avatar of Camillia
CamilliaFlag for United States of America

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:

foreach (ListItem item in ddlArea.Items)
            {
               if (item.Text == area.Name)
                {
                    ddlArea.SelectedValue = item.Value;
 
 
                    break;
                }
            }

Open in new window

Avatar of Joel Coehoorn
Joel Coehoorn
Flag of United States of America image

At what point in the page lifecycle does this run?  Page Load?  An event?  Earlier?  

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?
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;
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
Looks like an unbounded winforms control. Is it?
In that case you should use just SelectedItem.
Avatar of Camillia

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??
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
Avatar of burakiewicz
burakiewicz
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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..

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
the first one worked, thanks.
ddlArea.SelectedValue = ddlArea.Items.FindByText(area.Name).Value;