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

LVL 8
CamilliaAsked:
Who is Participating?
 
burakiewiczConnect With a Mentor Commented:
ddlArea.SelectedValue = ddlArea.Items.FindByText(area.Name).Value;
0
 
Joel CoehoornDirector of Information TechnologyCommented:
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?
0
 
burakiewiczCommented:
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;
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
iHadiCommented:
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
0
 
Jaime OlivaresSoftware ArchitectCommented:
Looks like an unbounded winforms control. Is it?
In that case you should use just SelectedItem.
0
 
CamilliaAuthor Commented:
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??
0
 
burakiewiczCommented:
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
0
 
CamilliaAuthor Commented:
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..

0
 
CamilliaAuthor Commented:
burakiewicz: -- just saw your second msg. Let me try that.
0
 
burakiewiczCommented:
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
0
 
CamilliaAuthor Commented:
the first one worked, thanks.
ddlArea.SelectedValue = ddlArea.Items.FindByText(area.Name).Value;
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.