473,513 Members | 2,399 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1268
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
10176
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
18514
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
2774
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
24083
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
718
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
3768
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
2732
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
3587
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
3604
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
7166
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7386
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7543
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7534
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5689
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5094
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
3236
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1601
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
459
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.