People for some reason still appear to be falling foul of this one in the newsgroups. How do I add spaces into the value displayed in listbox or to a DropDownList in ASP.NET.
One of the problems is that people insist on trying to use a space chr(32) which does not have an corresponding HTML value, so you technically cannot use ' ' as most people try.
A wee snippet of code to help you out.
for(int i = 0 ;i<=10;i++){
DropDownList1.Items.Add(new ListItem("Item " + insertSpaces (5) + i.ToString(),i.ToString() ));
}
....and a method call to add the spaces
string insertSpaces(int numOfS){
string S = "";
for (int i = 0; i <= numOfS; i++){
S += " ";
}
return Server.HtmlDecode(S);
}
The DropDownList renders to an HTML Select tag and the spaces are rendered as ANSI chr(160), or Unicode no break space for us mere mortals!
<select name="DropDownList1" id="DropDownList1">
<option value="0">Item      0</option>
</select>
Bear in mind that the concept of a space character (chr(32) has no HTML equivalent which is why the decode converts the html into its decoded value of   or unicode no-break space.