Recently, I was very depressed. I found a server control on the Internet in a drop-down box that I can enter for several days. I used it and found that it could not meet my own needs. As a result, it took several hours to make the changes and finally took shape. This control differs from others in the following two aspects:
1. Generally, dropdownlist selects the first item by default. In this case, there will be no event when selecting the first item. In this case, we usually need to trigger the ongchange event, so I forced the selectedindex change in its foucus event.
2. If the entered content is the same as an item in the drop-down box, the item in the drop-down box is automatically selected.
Using system;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. componentmodel;
Using system. collections;
Namespace extendwebcontrols
{
[Toolboxdata ("<{0}: dropdownlistextend runat = \" Server \ "/>")]
Public class dropdownlistextend: system. Web. UI. webcontrols. textbox
{
Private hashtable _ values;
Private dropdownlist _ dropdownlist;
Public dropdownlistextend ()
{
_ Values = new hashtable ();
_ Dropdownlist = new dropdownlist ();
}
Public hashtable values
{
Get {return _ values ;}
Set {_ values = value ;}
}
Protected override void render (htmltextwriter output)
{
Int iwidth = convert. toint32 (base. Width. value );
If (iwidth = 0)
{
Iwidth = 102;
Base. width = unit. parse ("102px ");
}
Int swidth = iwidth + 16;
Int spanwidth = swidth-18;
Output. Write ("<Div style = \" position: relative \ "> ");
Output. Write ("<span style = \" margin-left: "+ spanwidth. tostring () +" PX; overflow: hidden; width: 18px \ "> ");
_ Dropdownlist. width = unit. parse (swidth. tostring () + "PX ");
_ Dropdownlist. style. Add ("margin-left", "-" + spanwidth. tostring () + "PX ");
_ Dropdownlist. ID = base. ID + "_ select ";
_ Dropdownlist. Attributes. Add ("onchange", "This. parentnode. nextsibling. value = This. Value ");
_ Dropdownlist. Attributes. Add ("onfocus", "" + this. getfocusscript () + "");
If (_ values. Count> 0)
{
Foreach (string key in _ values. Keys)
{
Listitem item = new listitem ();
Item. value = key;
Item. Text = _ values [Key]. tostring ();
_ Dropdownlist. Items. Add (item );
}
}
_ Dropdownlist. rendercontrol (output );
Output. Write ("</span> ");
Base. style. Clear ();
Base. width = unit. parse (iwidth. tostring () + "PX ");
Base. style. Add ("Left", "0px ");
Base. style. Add ("position", "absolute ");
Base. Render (output );
Output. Write ("</div> ");
}
Private string getfocusscript ()
{
String strscript = "\ n ";
Strscript + = "Var isexist =-2; \ n ";
Strscript + = "Var OBJ = event. srcelement; \ n ";
Strscript + = "Var STR = This. parentnode. nextsibling. value; \ n ";
Strscript + = "Var ary = obj. options; \ n ";
Strscript + = "for (VAR I = 0; I <ary. length; I ++) {\ n ";
Strscript + = "If (STR = ary [I]. Text) {\ n ";
Strscript + = "isexist = I; \ n ";
Strscript + = "break; \ n ";
Strscript + = "} \ n ";
Strscript + = "} \ n ";
Strscript + = "If (isexist! =-2) {\ n ";
Strscript + = "obj. selectedindex = isexist; \ n ";
Strscript + = "} \ n ";
Strscript + = "else {\ n ";
Strscript + = "obj. selectedindex =-1; \ n ";
Strscript + = "} \ n ";
Return strscript;
}
}
}