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>
<OptionValue= "0">Level 00</Option>
<OptionValue= "1"> Level 01</Option>
<OptionValue= "2">Level 02</Option>
<OptionValue= "3">Level 03</Option>
<OptionValue= "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>
<OptionValue= "0">Level 00</Option>
<OptionValue= "1">&Nbsp; Level 01</Option>
<OptionValue= "2">&Nbsp;&Nbsp; Level 02</Option>
<OptionValue= "3">&Nbsp;&Nbsp;&Nbsp; Level 03</Option>
<OptionValue= "4">&Nbsp;&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 reload the control, and change "& nbsp;" back to "" during Render "". Probably:ClassXxxDropDownList: DropDownList
{
Protected Override VoidRender (HtmlTextWriter writer)
{
StringBuilder strb= NewStringBuilder ();
StringWriter sw= NewStringWriter (strb );
HtmlTextWriter htw= NewHtmlTextWriter (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:CharNbsp=(Char)0xA0;
For(IntI=0; I< 5;++I)
{
Ddl3.Items. Add (NewListItem ("Level 0". PadLeft (I+7, Nbsp)+I, I. ToString ()));
}
The generated client code is:<Select>
<OptionValue= "0">Level 00</Option>
<OptionValue= "1">Level 01</Option>
<OptionValue= "2">Level 02</Option>
<OptionValue= "3">Level 03</Option>
<OptionValue= "4">Level 04</Option>
</Select>