<summary>///drop-down Control///</summary> [ToolBoxData ("<{0}:multipledropdownlist runat=server> ;</{0}:multipledropdownlist> ")] public class Multipledropdownlist:compositecontrol {#region Field
s private CheckBoxList _checkboxes;
Private TextBox _textbox;
private static ListItem _selectallitem = new ListItem ("Select All", "___selectall___"); #endregion #region Data Binding Properties Public Object DataSource {get {return viewstate["_datasou Rce "];
set {viewstate["_datasource"] = value;} public string DataTextField {get {viewstate["DataTextField"] = = null? "": viewstate["DataTextField"]. ToString ();
set {viewstate["DataTextField"] = value;} public string DataValueField {get {viewstate["DataValueField"] = = null? "": viewstate["DataValueField"]. ToString ();
} set {viewstate["DataValueField"] = value;} public string Datatextformatstring {get {viewstate["datatextformatstring"] = = NULL ? "": viewstate["datatextformatstring"]. ToString ();
set {viewstate["datatextformatstring"] = value;}
#endregion #region Properties public ienumerable<string> selectedvalues { get {foreach (ListItem item in SelectedItems) {Yie LD return item.
Value;
}} public ienumerable<listitem> SelectedItems {get {foreach (ListItem item in _checkboxes.items) {if Isselectedallitem (item))
Continue if (item.
Selected) yield return item; }}///<summary>///a comma-separated selectionThe value in///</summary> public String selectedvaluesjoined {get {return string.join (",", Selectedvalues.toarray ());
#endregion public override void DataBind () {ensurechildcontrols ();
_checkboxes.datasource = DataSource;
_checkboxes.datatextfield = DataTextField;
_checkboxes.datavaluefield = DataValueField;
_checkboxes.datatextformatstring = datatextformatstring; Base. DataBind ()//Because of this bind, the bind of its child controls is recursive, so Insert full selection to the bottom to prevent the data from being emptied before bind _checkboxes.items.insert (0, _selectallitem)
; } protected override void CreateChildControls () {base.
CreateChildControls ();
_textbox = new TextBox ();
if (width.value>0) _textbox.width = Width;
Controls.Add (_textbox);
The container for the dropdown box HtmlGenericControl div = new HtmlGenericControl ("div"); Div. StyLe.
ADD (Htmltextwriterstyle.backgroundcolor, "white"); Div.
Style.add (Htmltextwriterstyle.display, "none"); Div.
Style.add (htmltextwriterstyle.position, "absolute");
Use CheckBoxList as multiple-selection _checkboxes = new CheckBoxList ();
_checkboxes.repeatdirection = repeatdirection.vertical;
_checkboxes.repeatlayout = RepeatLayout.Flow; Div.
Controls.Add (_checkboxes);
Controls.Add (DIV); } protected override void AddAttributesToRender (HtmlTextWriter writer) {base.
AddAttributesToRender (writer); Writer.
AddAttribute (Htmltextwriterattribute.class, "__multipledropdownlist__"); Writer.
AddStyleAttribute (Htmltextwriterstyle.display, "block"); Writer.
AddStyleAttribute ("float", "left"); } protected override void OnInit (EventArgs e) {base.
OnInit (e); Contains js var jsurl = page.cliEntscript.getwebresourceurl (this. GetType (), "CSLC."
Athena.WebExtendControl.MultipleDropdownList.js "); This.
Page.ClientScript.RegisterClientScriptInclude ("Multipledropdownlist_js", jsurl); Contains css var cssurl = Page.ClientScript.GetWebResourceUrl (this. GetType (), "CSLC."
Athena.WebExtendControl.MultipleDropdownList.css ");
String csslink = "<link href= '" +cssurl + "' rel= ' stylesheet ' type= ' ' text/css '"/> ";
LiteralControl include = new LiteralControl (csslink); This.
PAGE.HEADER.CONTROLS.ADD (include); //Whether an option is a "Select All" item, private bool Isselectedallitem (ListItem Item) {return item. Value = = _selectallitem.value && item.
Text = = _selectallitem.text; }
}
Above for C # source code, JS code does not post, see source Codes
Use examples
<cc1:multipledropdownlist id= "MultipleDropdownList1" runat= "Server"/>
var procducts = new List<procduct> ()
{
new procduct () {id=1,name= "N72"},
new Procduct () {id=2,name= " N95 "}
};
Multipledropdownlist1.datasource = procducts;
Multipledropdownlist1.datatextfield = "Name";
Multipledropdownlist1.datavaluefield = "Id";
Multipledropdownlist1.databind ();
The effect chart is as follows:
Download source