pre-selecting generated items in DropDownList (C#)

K

Kevin Blount

I'm using the following code to create a DropDownList based on the
contents of a text file, which works. Now I want to add another feature,
which is where I need some guidance.

On one page I present this DropDownList asking people to select a
country, but in another page I want to present the list, with their
previously selected option as the currently selected one. How can I do that?



//COUNTRY LIST SETUP
System.IO.StreamReader countriesSR = new
System.IO.StreamReader(Server.MapPath("/_scripts/resources/" +
pageLanguage + "_countries.txt"));

string nextCountryLine = null;
string nextCountryText = string.Empty;
string nextCountryValue = string.Empty;

while ((nextCountryLine = countriesSR.ReadLine()) != null)
{
string[] countryProperties = new string[2];
countryProperties = nextCountryLine.Split(',');
nextCountryText = countryProperties[0].Trim();
nextCountryValue = countryProperties[1].Trim();

ListItem nextCountryItem = new ListItem(nextCountryText,nextCountryValue);

member_country.Items.Add(nextCountryItem);
}
countriesSR.Close();

TIA

Kevin
 
M

mark.norgate

Use the following:

member_country.SelectedIndex = <index of selected item>

I think you have to loop around the items in the list, looking for the
selected value, until you find a match, then set the selected index to
the item with the correct value.

Mark

I haven't checked that all these as "set"able, but one of them should
work.
 
C

carl

Kevin,

Pass the selected value of your dropdownlist to the next page on the
query string (ie, mypage.aspx?co=US), and do this to select it on the
next page:
myDropDownList.Items.FindByValue(Request.QueryString["co"]).Selected =
true;

Also, check out the ODBC text file driver:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/odbc/htm/odbcjetschema_ini_file.asp

You can read your text file into a dataset and take advantage of
databinding, filtering, etc. It's really easy to configure and if you
ever move the file to a database there's a lot less code to change.

-Carl
 
M

Marc Gravell

The other replies should answer your question - I just wanted to
observe that in the following:
string[] countryProperties = new string[2];
countryProperties = nextCountryLine.Split(',');

The "= new string[2];" is unnecessary, and creates an object that is
immediately orphaned; you may as well:

string[] countryProperties = nextCountryLine.Split(',');

Also - you may find it improves performance to keep a list of the new
items when looping, and then use an AddRange approach to add them all
in one go; depending on the implementation, this can avoid unnecessary
multiple layout operations.

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top