Padding Listboxes with Spaces#

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 += "&nbsp;";

}

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 &#160;&#160;&#160;&#160;&#160;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 &nbsp; into its decoded value of &#160; or unicode no-break space.

 

1/3/2007 2:05:50 PM (GMT Standard Time, UTC+00:00) #    Comments  |  Trackback

 

All content © 2009, John Timney
On this page
This site
Calendar
<January 2009>
SunMonTueWedThuFriSat
28293031123
45678910
11121314151617
18192021222324
25262728293031
1234567
Archives
Sitemap
Blogroll OPML
Talk to Me

The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

Send mail to the author(s) E-mail