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
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
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).selecte d = true
exit for
end if
next
end if
end sub
------------------- somewhere down in the <HTML>
<asp:DropDownList ID="StateList" Runat="Server" />
(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).selecte
exit for
end if
next
end if
end sub
------------------- somewhere down in the <HTML>
<asp:DropDownList ID="StateList" Runat="Server" />
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
http://www.burgessconsulting.com/aspnet/aspnet/databind.aspx
Cheers!!