By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,894 Members | 1,942 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,894 IT Pros & Developers. It's quick & easy.

pre-selecting generated items in DropDownList (C#)

P: n/a
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
Sep 22 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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.

Kevin Blount wrote:
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
Sep 22 '06 #2

P: n/a
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.QueryStri ng["co"]).Selected =
true;

Also, check out the ODBC text file driver:
http://msdn.microsoft.com/library/de...a_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
Kevin Blount wrote:
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?
Sep 22 '06 #3

P: n/a
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

Sep 23 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.