Solved

Initialize Drop-Down with current value

Posted on 2003-10-24
3
610 Views
Last Modified: 2007-12-19
I'm new to ASP.Net and C#. I have a drop-down control to select a State, which has all the state values loaded into it. The 'value' of each of my items is  the 2-character state abbreviation. The 'text' is the full state name.

On one of my pages, I want to display the current data from the database and let the user edit it.

How do I get the current state to show in the drop-down control? I think I need to match my existing value to one of the "values" in the drop-down, but I'm not sure what the syntax is for going through the values and finding the matching one.

I'd like to get an answer soon...

thanks, Ted
0
Comment
Question by:tk3
3 Comments
 
LVL 21

Expert Comment

by:ap_sajith
Comment Utility
May be this might be of help...

http://www.burgessconsulting.com/aspnet/aspnet/databind.aspx

Cheers!!
0
 
LVL 12

Expert Comment

by:kingsfan76
Comment Utility
maybe not the best way, but this works for me for pre-selecting a list item in ASP.NET:
(assume you already correctly populate the drop-down countrol with the states)

Sub Page_Load
  if not IsPostBack then
     dim theState
     dim i
     theState = "CA"    'i suppose you would get this value from database, i just use "CA" here
     for i = 0 to StateList.Items.Count - 1
        if StateList.Items(i).value = theState then
            StateList.Items(i).selected = true
            exit for
        end if
     next
  end if
end sub

------------------- somewhere down in the <HTML>

<asp:DropDownList ID="StateList" Runat="Server" />  
0
 

Accepted Solution

by:
spike7397 earned 500 total points
Comment Utility
Try iterating through the listitems in the drop down list.
in C# it would be
foreach (ListItem tmpItem in YourDropDownList)
{
   Check the value they selected with the current list item and store the index of the drop down list item that has the same value in a temporary integer (tmpInteger)
}
YourDropDownList.SelectedIndex = tmpInteger;

Hope that helps!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:  The Exchange of information …
Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
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: …

744 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