generate <asp:ListItem> with dynamic content

K

Kevin Blount

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 <select> and <option> tags 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
 
T

Teemu Keiski

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
 
K

Kevin Blount

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
 
S

sloan

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.
 
K

Kevin Blount

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,999
Messages
2,570,244
Members
46,838
Latest member
KandiceChi

Latest Threads

Top