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

generate <asp:ListItem> with dynamic content

P: n/a
As mentioned before, I'm creating a multi-lingual page where the text of
the page comes from a database. This page includes a registration form
which asks for address information, including the Country.

I have 6 .txt files that contain a complete list of countries that we
sell to (65 in total), each written in the appropriate way for the
language of the site, e.g. Mexico for English US and UK sites and México
for the Spanish language site.

I want to add these countries to a ListBox, changing the .Text of the
ListItem depending on which language the pages is being viewed it.

The problem I think I'll face is that I will have to manually specify 65
ListItem tags and then dynamically set the .Value and .Text for each
one, and that's a lot of work (but if it has to be done, so be it).

I'm wondering if there might be a way to dynamically add new ListItem
tags to my ListBox by opening the .txt file and going through it line by
line until I reach the end.

I have 3 ways/ideas in mind, and wanted to get feedback on them, and
potentially any help/examples of other ways:

1) manually code 65 ListItem tags, 65 "CountryItem45.Value =
txtFileValueEntry;" lines and 65 "CountryItem23.Text =
txtFileTextEntry;" lines;

2) use HTML <selectand <optiontags instead (can I use HTML form tags
inside my ASP.NET runat=server form that uses asp:TextBox tags already,
i.e. mix between .NET and HTML in the same form?)

3) open the .txt file and for each line read create a new ListItem and
set it's .Value and .Text - this is the preferred, but I've no clue how
to get started with it.

any help would be greatly appreciated.

Kevin
Aug 25 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Can you give a hint of contents of one that type of file? essentially you'd
just read the file with StreamReader using ReadLine method. And in that loop
add ListItems, something like

Dim sr As StreamReader = Nothing
Try
sr= New StreamReader(path)

Do While sr.Peek() >= 0
Dim linetext As String = sr.ReadLine()

'Here get text and value out of the one line string...I
don't know the file structure so I cannot demonstrate that
Dim text As String = ...
Dim value As String = ...

'Creating and adding ListItem
Dim litem As New ListItem(text,value)
ListBox1.Items.Add(litem)
Loop
Finally
If Not sr Is Nothing Then sr.Close()
End try
--
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU
http://blogs.aspadvice.com/joteke

"Kevin Blount" <ke**********@LOLgmail.comwrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
As mentioned before, I'm creating a multi-lingual page where the text of
the page comes from a database. This page includes a registration form
which asks for address information, including the Country.

I have 6 .txt files that contain a complete list of countries that we sell
to (65 in total), each written in the appropriate way for the language of
the site, e.g. Mexico for English US and UK sites and México for the
Spanish language site.

I want to add these countries to a ListBox, changing the .Text of the
ListItem depending on which language the pages is being viewed it.

The problem I think I'll face is that I will have to manually specify 65
ListItem tags and then dynamically set the .Value and .Text for each one,
and that's a lot of work (but if it has to be done, so be it).

I'm wondering if there might be a way to dynamically add new ListItem tags
to my ListBox by opening the .txt file and going through it line by line
until I reach the end.

I have 3 ways/ideas in mind, and wanted to get feedback on them, and
potentially any help/examples of other ways:

1) manually code 65 ListItem tags, 65 "CountryItem45.Value =
txtFileValueEntry;" lines and 65 "CountryItem23.Text = txtFileTextEntry;"
lines;

2) use HTML <selectand <optiontags instead (can I use HTML form tags
inside my ASP.NET runat=server form that uses asp:TextBox tags already,
i.e. mix between .NET and HTML in the same form?)

3) open the .txt file and for each line read create a new ListItem and set
it's .Value and .Text - this is the preferred, but I've no clue how to get
started with it.

any help would be greatly appreciated.

Kevin

Aug 25 '06 #2

P: n/a
Hi Teemu,

Thanks for the reply. Your example shows the bits I needed, namely the
"ListBox1.Items.Add()" part.

The text files contains the following type of data:

esp
Mexico,México
Netherlands,Nederlands

where the first 'column' is the English spelling of a country, and the
second 'column' is the localized translation of that country.

or

French,Francés
German,Alemán
Dutch,Holandés

where first you get the English spelling of a language, and then the
localize spelling.

I'll be using the C# .Split() method to separate the two 'columns', but
assuming I end up with 2 strings called "engLang" and "localLang" or
"engCountry" and "engCountry" I think I would be updating your example
to use:

countryList.Items.Add(engLang,localLang)

for example. Would that be right?

I'm going to start putting this into my code now, so I'll probably get
it working, or sit waiting for your reply if I fail ;)

Thanks again

Kevin

Teemu Keiski wrote:
Can you give a hint of contents of one that type of file? essentially you'd
just read the file with StreamReader using ReadLine method. And in that loop
add ListItems, something like

Dim sr As StreamReader = Nothing
Try
sr= New StreamReader(path)

Do While sr.Peek() >= 0
Dim linetext As String = sr.ReadLine()

'Here get text and value out of the one line string...I
don't know the file structure so I cannot demonstrate that
Dim text As String = ...
Dim value As String = ...

'Creating and adding ListItem
Dim litem As New ListItem(text,value)
ListBox1.Items.Add(litem)
Loop
Finally
If Not sr Is Nothing Then sr.Close()
End try

Aug 25 '06 #3

P: n/a

ListItem li = new ListItem("North Carolina" , "NC");
ddlMyDropDownList.Items.Add(li);

The trick is the overloaded ListItem constructor.
or you can set the values like this:

ListItem lili = new ListItem();
lili.Text = "my text";
lili.Value = "MT";

something like that.

"Kevin Blount" <ke**********@LOLgmail.comwrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
As mentioned before, I'm creating a multi-lingual page where the text of
the page comes from a database. This page includes a registration form
which asks for address information, including the Country.

I have 6 .txt files that contain a complete list of countries that we
sell to (65 in total), each written in the appropriate way for the
language of the site, e.g. Mexico for English US and UK sites and México
for the Spanish language site.

I want to add these countries to a ListBox, changing the .Text of the
ListItem depending on which language the pages is being viewed it.

The problem I think I'll face is that I will have to manually specify 65
ListItem tags and then dynamically set the .Value and .Text for each
one, and that's a lot of work (but if it has to be done, so be it).

I'm wondering if there might be a way to dynamically add new ListItem
tags to my ListBox by opening the .txt file and going through it line by
line until I reach the end.

I have 3 ways/ideas in mind, and wanted to get feedback on them, and
potentially any help/examples of other ways:

1) manually code 65 ListItem tags, 65 "CountryItem45.Value =
txtFileValueEntry;" lines and 65 "CountryItem23.Text =
txtFileTextEntry;" lines;

2) use HTML <selectand <optiontags instead (can I use HTML form tags
inside my ASP.NET runat=server form that uses asp:TextBox tags already,
i.e. mix between .NET and HTML in the same form?)

3) open the .txt file and for each line read create a new ListItem and
set it's .Value and .Text - this is the preferred, but I've no clue how
to get started with it.

any help would be greatly appreciated.

Kevin

Aug 25 '06 #4

P: n/a
Thanks for the response, sloan.

Following Teemu's post I got busy, and here's the result

(and it even works!!)

<% System.IO.StreamReader sr = new
System.IO.StreamReader(Server.MapPath("test.txt")) ;

string nextLine = null;
string nextText = string.Empty;
string nextValue = string.Empty;
int iCommaPos, iLength;
while ((nextLine = sr.ReadLine()) != null)
{
string[] itemProperties = new string[2];
itemProperties = nextLine.Split(',');

nextText = itemProperties[0].Trim();
nextValue = itemProperties[1].Trim();

ListItem nextItem = new ListItem(nextText,nextValue);
member_country.Items.Add(nextItem);
}
sr.Close();
%> <asp:ListBox ID="member_country" runat="server"
SelectionMode="single" Rows="1" Width="300"></asp:ListBox>
plus... I must be actually learning something, cause I was able to take
Teemu's VB code and change it to C#!! hehe

thanks both.. have great weekends

Kevin

sloan wrote:
ListItem li = new ListItem("North Carolina" , "NC");
ddlMyDropDownList.Items.Add(li);

The trick is the overloaded ListItem constructor.
or you can set the values like this:

ListItem lili = new ListItem();
lili.Text = "my text";
lili.Value = "MT";

something like that.
Aug 25 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.