views:

477

answers:

3

I am trying to insert items into a listbox in my asp.net C# application

I concatenate some values and put whitespace in between them but it doesn't show up in the listbox.

        ListItem lt = new ListItem();
        lt.Text = ItemName + "    " + barcode + "    " + price; // problem
        lt.Value = barcode;
        lstMailItems.Items.Add(lt);

i even tried

lt.Text = ItemName + "\t\t" + barcode + "\t\t" + price; // problem
lt.Text = ItemName + "& nbsp;" + barcode + "& nbsp;" + price; // &nbsp shows up as text

but that even doesn't seem to work. How can I put whitespace between these strings so that it shows up in the listbox as well

+1  A: 
Myra
That doesnt work either
Jaelebi
@Myra: He need spaces in listitem
Muhammad Akhtar
  shows up cleartext
Jaelebi
Doesnt work. Its not reading HTML
Jaelebi
Are you using white-spacing attribute of css in your page?
Myra
A: 
        string spaces = "& nbsp;& nbsp;& nbsp;& nbsp;"; // without spaces after &
        spaces = Server.HtmlDecode(spaces);

        lt.Text = ItemName + "spaces" + barcode + "spaces" + price; // works
Jaelebi
A: 

Here are two examples that work well, and how to get the current formatted:

  var SaleItem = new
    {
        name = "Super Cereal",
        barcode = "0000222345",
        price = 2.55m
    };

    ListItem lt = new ListItem();
    string space = " ";
    lt.Text = String.Concat(SaleItem.name, 
        space, SaleItem.barcode, space, SaleItem.price);
    lt.Value = SaleItem.barcode;

    ListItem lt2 = new ListItem();
    lt2.Text = string.Copy(String.Format("{0}: {1} {2}", 
               SaleItem.name, SaleItem.barcode, SaleItem.price.ToString("C")));
    lt2.Value = SaleItem.barcode;

    lstMailItems.Items.Add(lt);
    lstMailItems.Items.Add(lt2);
Jim Schubert