Solved

Initialize Drop-Down with current value

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Update Stored Procedure question 8 48
Classic ASP, Search Recordset on pick list change 1 52
format nvarchar field as mm/dd/yyyy 4 61
Select2 jquery help 9 74
Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
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/…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

911 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

20 Experts available now in Live!

Get 1:1 Help Now