Solved

Initialize Drop-Down with current value

Posted on 2003-10-24
3
650 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 500 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ASP Focus problem 3 71
rebind a grid after user clicks on node in treeview 1 48
MS SQL + date 6 41
Extracting nodes  using classical ASP 3 23
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 demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

713 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