473,323 Members | 1,550 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,323 software developers and data experts.

pre-selecting generated items in DropDownList (C#)

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
3 1580
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Headless | last post by:
I've marked up song lyrics with the <pre> tag because it seems the most appropriate type of markup for the type of data. This results in inefficient use of horizontal space due to UA's default...
7
by: Alan Illeman | last post by:
How do I set several different properties for PRE in a CSS stylesheet, rather than resorting to this: <BODY> <PRE STYLE="font-family:monospace; font-size:0.95em; width:40%; border:red 2px...
2
by: Buck Turgidson | last post by:
I want to have a css with 2 PRE styles, one bold with large font, and another non-bold and smaller font. I am new to CSS (and not exactly an expert in HTML, for that matter). Is there a way to...
5
by: Porthos | last post by:
I'm authoring an XML document and using the <pre> html tag for the portions that are not dynamically generated. The <pre> text is displaying in a smaller font size (and I believe different font)...
5
by: Michael Shell | last post by:
Greetings, Consider the XHTML document attached at the end of this post. When viewed under Firefox 1.0.5 on Linux, highlighting and pasting (into a text editor) the <pre> tag listing will...
8
by: Jarno Suni not | last post by:
It seems to be invalid in HTML 4.01, but valid in XHTML 1.0. Why is there the difference? Can that pose a problem when such a XHTML document is served as text/html?
7
by: Rocky Moore | last post by:
I have a web site called HintsAndTips.com. On this site people post tips using a very simply webform with a multi line TextBox for inputing the tip text. This text is encode to HTML so that no...
23
by: Xah Lee | last post by:
The Concepts and Confusions of Pre-fix, In-fix, Post-fix and Fully Functional Notations Xah Lee, 2006-03-15 Let me summarize: The LISP notation, is a functional notation, and is not a...
14
by: Schraalhans Keukenmeester | last post by:
I am building a default sheet for my linux-related pages. Since many linux users still rely on/prefer viewing textmode and unstyled content I try to stick to the correct html tags to pertain good...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.