Solved

SelectedItem in dropdownlist

Posted on 2008-06-23
11
1,210 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
XML & .net 5 41
C# replace string in void with dR["myData"].ToString() 3 41
Name space syntax error 12 43
SQL Query Producing decimal places when it shouldn;t be 8 35
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

920 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now