Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Initialize Drop-Down with current value

Posted on 2003-10-24
3
Medium Priority
?
673 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
ID: 9618813
May be this might be of help...

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

Cheers!!
0
 
LVL 12

Expert Comment

by:kingsfan76
ID: 9619201
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 1500 total points
ID: 9627116
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
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 informatio…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Screencast - Getting to Know the Pipeline
Suggested Courses

876 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