Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inserting 'space' in an ASP.NET DropDownList Control

I have an ASP.NET DropDownList control that retrieves the first and last name from a column in the database. Instead of having just one space between the first and last name, I want there to be three. How do I add the extra spaces between the two pieces of text in the DropDownList?

like image 757
jumbojs Avatar asked Sep 14 '09 15:09

jumbojs


2 Answers

Add   instead of space character, and HtmlDecode all elements after binding :

string[] items = new string[] { 
    "name& nbsp;& nbsp;& nbsp;surname1", 
    "name& nbsp;& nbsp;& nbsp;surname2" };

ddl.DataSource = items;
ddl.DataBind();

foreach (ListItem item in ddl.Items)
{
    item.Text = HttpUtility.HtmlDecode(item.Text);
}
like image 152
Canavar Avatar answered Nov 01 '22 17:11

Canavar


I had this same question, thanks. I did however want to offer one suggestion.

If there's any chance you have an ampersand (&) or other special characters in your data, doing it this way is going to result in your name look like John &amp Jane instead of John & Jane.

So might be better to do it this way:

ListItem li = new ListItem("Fname" +HttpUtility.HtmlDecode("   ") +"Lname", id);
ddlSearchCategories.Items.Add(li);

Might want to use String.Format instead of +

like image 43
merk Avatar answered Nov 01 '22 19:11

merk