Solved

SelectedItem in dropdownlist

Posted on 2008-06-23
11
1,213 Views
Last Modified: 2008-06-23
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

0
Comment
Question by:Camillia
11 Comments
 
LVL 18

Expert Comment

by:jcoehoorn
ID: 21849671
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
 
LVL 16

Expert Comment

by:burakiewicz
ID: 21849677
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
 
LVL 13

Expert Comment

by:iHadi
ID: 21849696
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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 21849724
Looks like an unbounded winforms control. Is it?
In that case you should use just SelectedItem.
0
 
LVL 7

Author Comment

by:Camillia
ID: 21849766
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
 
LVL 16

Expert Comment

by:burakiewicz
ID: 21849786
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
 
LVL 16

Accepted Solution

by:
burakiewicz earned 500 total points
ID: 21849796
ddlArea.SelectedValue = ddlArea.Items.FindByText(area.Name).Value;
0
 
LVL 7

Author Comment

by:Camillia
ID: 21849815
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
 
LVL 7

Author Comment

by:Camillia
ID: 21849822
burakiewicz: -- just saw your second msg. Let me try that.
0
 
LVL 16

Expert Comment

by:burakiewicz
ID: 21849838
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
 
LVL 7

Author Comment

by:Camillia
ID: 21849876
the first one worked, thanks.
ddlArea.SelectedValue = ddlArea.Items.FindByText(area.Name).Value;
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question