When you use the SELECT element drop-down list on the Web page to display the data list, we sometimes encounter data entries with layers. For example, the Sub-Forum in the Forum and its classification, as well as some hierarchical data entries with the inclusion relationship. Make the drop-down list box show certain indentation for different levels, which is a friendly typographical method.
It is very easy to create a drop-down list window in HTML compiling or in ASP or other scripting languages. We know that spaces "" are automatically ignored by the brower Display engine before and after the Option label, so we can use hard spaces. The effect is as follows:
<Select>
<Option value = "0"> Level 00 </option>
<Option value = "1"> Level 01 </option>
<Option value = "2"> Level 02 </option>
<Option value = "3"> Level 03 </option>
<Option value = "4"> Level 04 </option>
</Select>
There seems to be nothing to say if it is so simple to achieve this effect. However, when we use the Server Control ListBox or DropDownList in ASP. NET, the problem of achieving this effect comes. When the Text attribute of the ListItem class is output as HTML code, HtmlEncode is automatically converted. The preceding example is output as follows:
<Select>
<Option value = "0"> Level 00 </option>
<Option value = "1"> & nbsp; Level 01 </option>
<Option value = "2"> & nbsp; Level 02 </option>
<Option value = "3"> & nbsp; Level 03 </option>
<Option value = "4"> & 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 reload the control, and change "& nbsp;" back to "" during Render "". Probably:
Class XxxDropDownList: DropDownList
{
Protected override void Render (HtmlTextWriter writer)
{
StringBuilder strb = new StringBuilder ();
StringWriter sw = new StringWriter (strb );
HtmlTextWriter htw = new HtmlTextWriter (sw );
Base. Render (htw );
Strb. Replace ("& nbsp ;","");
Writer. Write (strb. ToString ());
}
}
This solution has many problems, and efficiency is one thing and is very incomplete. In addition to using this "brutal" method to modify the Render result, there is also a uugly method that uses the full-angle Space, that is "". However, in a Chinese system, this method seems to have been mentioned in the past. However, in a pure English (Not support East-Asian language) environment, such Option entries will become dizzy, the error result is as follows:
// The Left is in the Chinese system, and the right is in the pure English system.
So what should we do? Reload ListItem ?! However, the ListItem class is modified by sealed. Later I found that the "original form" can be used to let DropListBox output correct HTML code. What is the original form? This is the named entity defined in RFC 1866. Its full name is no-break space. The CDATA format is :. We put 160 as a character in the Text of ListItem to get the correct HTML output. The effect is the same as that of the HTML example in the first figure. The server code is:
Char nbsp = (char) 0xA0;
For (int I = 0; I <5; ++ I)
{
Ddl3.Items. Add (new ListItem ("Level 0". PadLeft (I + 7, nbsp) + I, I. ToString ()));
}
The generated client code is:
<Select>
<Option value = "0"> Level 00 </option>
<Option value = "1"> Level 01 </option>
<Option value = "2"> Level 02 </option>
<Option value = "3"> Level 03 </option>
<Option value = "4"> Level 04 </option>
</Select>