.Net ------ 下拉框多选
如图:在下拉框中可以多选
实现:
1、引用
<%@ Register src="../../CommonDrops/ddlMoreSelect/WebddlMoreSelect.ascx" tagname="WebddlMoreSelect" tagprefix="uc2" %>
2、使用
<uc2:WebddlMoreSelect ID="WebddlMoreSelect1" runat="server" />
3、后台赋值
private void ddlLoad() { {//技术人员集合 ITEM.Inherits.Bll.qdCnt4.bllFourAdminUser _bllUser = new bllFourAdminUser(); List<ITEM.Model.qdCnt4.FourAdminUser> mList = _bllUser.GetListSelFourAdminUser3("AdminGuid,LoginID", base.EnterpriseGuid, " and RoleGuid in(select RoleGuid from tbFourAdminRole where RoleTypeFlag in(410,480))"); Dictionary<string, string> dic = new Dictionary<string, string>(); for (int i = 0; i <= mList.Count - 1; i++) { dic.Add(mList[i].AdminGuid, mList[i].LoginID); } this.WebddlMoreSelect1.Set_DataList_Ini(dic, 160, 200); } }
该用户控件所在位置
控件代码
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebddlMoreSelect.ascx.cs" Inherits="CommonDrops_ddlMoreSelect_WebddlMoreSelect" %> <%@ Register TagPrefix="cc1" Namespace="UNLV.IAP.WebControls" Assembly="DropDownCheckList" %> <link href="styles.css" rel="stylesheet" /> <link href="../../qdAdmin/qdcss/Style02.css" rel="stylesheet" /> <asp:Literal ID="Literal1" runat="server"></asp:Literal> <table class="ttable_border4_1"><tr><td> <cc1:DropDownCheckList id="DropDownCheckList3" runat="server" DropImagePosition="Left" DropImageSrc="../../qdAdmin/qdimages/duoxuan_blue.png" CssClass="ttable_border_none" DisplayTextCssClass="displayText3X" DisplayBoxCssStyle="checkText3" DisplayBoxCssClass="displayBox3X" CheckListCssStyle="checkText3" CheckListCssClass="checklist3" TextWhenNoneChecked="可多选,请选择..." DataMember="category" DataTextField="text" DataValueField="value" Separator="," ClientCodeLocation="../../CommonDrops/ddlMoreSelect/DropDownCheckList.js" ></cc1:DropDownCheckList></td></tr></table>
后台
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; using System.Text; public partial class CommonDrops_ddlMoreSelect_WebddlMoreSelect : System.Web.UI.UserControl { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { } } /// <summary> /// 控件多选数据初始化 /// </summary> /// <param name="dic"></param> public void Set_DataList_Ini(Dictionary<string, string> dic, int iWidth, int iHeight) { string _Width = iWidth.ToString() + "px"; string _Height = iHeight.ToString() + "px"; StringBuilder sb = new StringBuilder(); sb.Append("<style>"); sb.Append(".displayBox3 { border: 1px dashed white; cursor: pointer; background-color: white;}"); sb.Append(".displayBox3 td{ background-color: white; border-bottom-width:1px; border-bottom-color:red;}"); sb.Append(".displayText3 {background-color: white; border: 1px solid #808080; padding: 4px;width:" + _Width + "; border-bottom-width:1px; border-bottom-color:red; }"); sb.Append(".checkText3 {background-color: white; border: 1px solid #808080; padding: 4px;width: " + _Width + "; border-bottom-width:1px; border-bottom-color:red; }"); sb.Append(".checklist3 { background-color: white; border: 1px solid #808080; padding: 4px; height: " + _Height + "; width: " + _Width + "; overflow: scroll; }"); sb.Append("</style>"); this.Literal1.Text = sb.ToString(); DataTable dt = new DataTable(); dt.Columns.Add("text"); dt.Columns.Add("value"); foreach (string key in dic.Keys) { DataRow drow = dt.NewRow(); drow["value"] = key; drow["text"] = dic[key]; dt.Rows.Add(drow); } this.DropDownCheckList3.DataSource = dt; this.DropDownCheckList3.DataBind(); } public void Set_Enabled(bool flag) { this.DropDownCheckList3.Enabled = false; } public string Get_CheckList_Values() { string s = string.Empty; for (int i = 0; i <= this.DropDownCheckList3.Items.Count - 1; i++) { if (this.DropDownCheckList3.Items[i].Selected == true) { string sign = s == string.Empty ? "" : ","; s += sign + this.DropDownCheckList3.Items[i].Value; } } return s; } public string Get_CheckList_Texts() { string s = string.Empty; for (int i = 0; i <= this.DropDownCheckList3.Items.Count - 1; i++) { if (this.DropDownCheckList3.Items[i].Selected == true) { string sign = s == string.Empty ? "" : ","; s += sign + this.DropDownCheckList3.Items[i].Text; } } return s; } public string _Values { get { return Get_CheckList_Values(); } set { Load_DataListbyValue(value); } } public string _Texts { get { return Get_CheckList_Texts(); } set { Load_DataListbyText(value); } } /// <summary> /// 初始数据Value值载入 /// </summary> /// <param name="s"></param> public void Load_DataListbyValue(string s) { s = s.Replace(",", ",").Replace("|", ",").Replace("~", ","); s = "," + s + ","; s = s.Replace(",,", ","); for (int i = 0; i <= this.DropDownCheckList3.Items.Count - 1; i++) { string svalue = "," + this.DropDownCheckList3.Items[i].Value + ","; if (s.IndexOf(svalue) >= 0) { this.DropDownCheckList3.Items[i].Selected = true; } } } /// <summary> /// 初始数据Text值载入 /// </summary> /// <param name="s"></param> public void Load_DataListbyText(string s) { s = s.Replace(",", ",").Replace("|", ",").Replace("~", ","); s = "," + s + ","; s = s.Replace(",,", ","); for (int i = 0; i <= this.DropDownCheckList3.Items.Count - 1; i++) { string svalue = "," + this.DropDownCheckList3.Items[i].Text + ","; if (s.IndexOf(svalue) >= 0) { this.DropDownCheckList3.Items[i].Selected = true; } } } }