When you display a list of data by using a Drop-down list box (SELECT Element) on a Web page, we sometimes encounter hierarchical data entries. For example, the sub-forum in the Forum and its classification, as well as some have a relationship with the hierarchical data entries. Make the Drop-down list box, the different level has a certain display indent will be very friendly one way of typesetting.
It is easy to make such a drop-down List window in HTML authoring State, or in scripting languages such as ASP. We know that the space "" in the option tag before and after the Brower will be automatically ignored by the display engine, so we use hard space on it, the effect of the following figure:
<Select>
<OptionValue= "0">Level 00</Option>
<OptionValue= "1">Level 01</Option>
<option value= "2"> Level </option>
<option value= "3"> Level </option>
<option value= "4"> Level </option>
</Select >
So simple to achieve this effect, there seems to be nothing to say. However, when we use the server control ListBox or DropDownList in ASP.net, the problem is to achieve this effect. Since the Text property of the ListItem class is automatically htmlencode converted when it is exported as HTML code, our example above is printed as:
<Select>
<OptionValue= "0">Level 00</Option>
<OptionValue= "1">  Level 01</Option>
<OptionValue= "2"> nbsp; level 02 </ option
; option value = "3" ; nbsp; nbsp; level 03 </ option
; option value = "4" ; nbsp; nbsp; nbsp; level 04 </ option
</ Select
This is really depressing, when I first encountered this problem, I used a very ugly method to solve it. is to overload the control, in the render " " Change back to "". It's probably:
ClassXxxdropdownlist:dropdownlist
{
ProtectedOverridevoidRender (HtmlTextWriter writer)
{
StringBuilder STRB=NewStringBuilder ();
StringWriter SW= New stringwriter (STRB);
htmltextwriter htw = new htmltextwriter (SW);
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; Base . Render (HTW);
strb . Replace ( " , "";
writer. Write (STRB. ToString ());
&NBSP;&NBSP;&NBSP;&NBSP;
There are many problems with this solution, efficiency is one thing, and very incomplete. In addition to using this "savage" method of modifying render results, there is also a ugly way to use full-width spaces, which is " "。 But in the Chinese system this method seems to also say the past, but in the pure English (not support East-asian language) environment, such option item is dizzy dish, the error effect is as follows figure:
//The left image is in the Chinese system, the right image is in the pure English system
So what do we do? Overload listitem?! But blocked, ListItem class is being SealedModified. I later discovered that the "raw form" that could be used would allow droplistbox to output the correct HTML code. What is the original form? This thing was originally defined in RFC 1866 named entity, the full name: No-break space,cdata format is:. We put 160 as a character in listitem text, we get the correct HTML output, and the effect is the same as the HTML example in the first picture. The server-side code is:
Charnbsp=(Char) 0xa0 ;
for ( int i = 0 ; i 5 ; + + i )
Span id= "Codehighlighter1_74_186_closed_text" > {
new listitem ( " level 0 " .) PadLeft (i + 7 , nbsp) + i, i.tostring ());
< /span>
The generated client code is:
<Select>
<OptionValue= "0">Level 00</Option>
<OptionValue= "1">Level 01</Option>
option Value = "2" level 02 </ option
&NBSP;&NBSP;&NBSP;&NBSP; Value = "3" level 03 </ option
&NBSP;&NBSP;&NBSP;&NBSP; Value = "4" level 04 </ option >
</ Select "