Solved

SelectedItem in dropdownlist

Posted on 2008-06-23
11
1,209 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Looks like an unbounded winforms control. Is it?
In that case you should use just SelectedItem.
0
 
LVL 7

Author Comment

by:Camillia
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 16

Expert Comment

by:burakiewicz
Comment Utility
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
Comment Utility
ddlArea.SelectedValue = ddlArea.Items.FindByText(area.Name).Value;
0
 
LVL 7

Author Comment

by:Camillia
Comment Utility
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
Comment Utility
burakiewicz: -- just saw your second msg. Let me try that.
0
 
LVL 16

Expert Comment

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

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

772 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

10 Experts available now in Live!

Get 1:1 Help Now