Link to home
Start Free TrialLog in
Avatar of tk3
tk3

asked on

Initialize Drop-Down with current value

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
Avatar of ap_sajith
ap_sajith

May be this might be of help...

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

Cheers!!
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" />  
ASKER CERTIFIED SOLUTION
Avatar of spike7397
spike7397

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial