473,406 Members | 2,312 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,406 software developers and data experts.

generate <asp:ListItem> with dynamic content

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
4 4647
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
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

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

Similar topics

0
by: CGuy | last post by:
Hi, I have a RadioButtonList control in my aspx page which shows two radiobuttons - "Yes" and "No". Since my page is intended for international audience, I would like the texts "Yes" and "No" to...
4
by: George | last post by:
I'm using .NET framework 1.1 and VS.NET 2003 for a aspx web form. There is a DropDwonList on the page and its SelectedIndexChanged event is fired to the server normally, until when I add another...
2
by: Simon Cheng | last post by:
Hi, For the following form: <form runat="server"> <asp:checkboxlist id="list" runat="server"> <asp:listitem runat="server" value="Cash" /> <asp:listitem runat="server" value="Check" />...
5
by: Mark Rae | last post by:
Hi, Has the <asp:DropDownList> - <asp:ListItem> functionality changed in v2? In v1.1, the following works as expected: <asp:DropDownList ID="cmbStatus" Runat=server EnableViewState=False>...
0
by: John Smith | last post by:
This is what I am trying to do: <asp:datagrid id="DataGrid1" runat="server" AutoGenerateColumns="False"> <Columns> <asp:BoundColumn Visible="False" DataField="id" ReadOnly="True"...
0
by: John Smith | last post by:
This is what I am trying to do: <asp:datagrid id="DataGrid1" runat="server" AutoGenerateColumns="False"> <Columns> <asp:BoundColumn Visible="False" DataField="id" ReadOnly="True"...
4
by: Kevin Blount | last post by:
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...
7
by: Jim in Arizona | last post by:
Using this: <asp:ListItem Value="0"</asp:ListItem> as long as I have a value listed and leave the area between the tags blank, when viewed the DropDownList will show a 0 as a choice. I want it...
4
by: justice750 | last post by:
Hi All, I am using a FormView control. The allows me to update records in the database. However, when a database field is null I can not update the field on the form. It works fine when the field...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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
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...

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.