2008-09-17

How to add spaces to ASP ListBox control

Problem:

When trying to indent some elements of the ListBox or DropDownList ASP controls with multiple spaces the browser only renders one space. Trying to add   instead of a space character renders the letters "&", "n", "b", "s", "p".

Solution:

Add Server.HtmlDecode(" ") instead of a simple space and everything should look fine.

private void FillList()
{
    listBox.Items.Add("Root");
    listBox.Items.Add(Server.HtmlDecode(" ") + "First level");
    listBox.Items.Add(Server.HtmlDecode("  ") + "Second level");
    listBox.Items.Add(Server.HtmlDecode("  ") + "Also second level");           
}

Post a Comment