先看看效果 准备数据 标准颜色图片从office Word的标准颜色窗口取出来并用进制把图片命名 图片尺寸X Pixel aspx: <asp:CheckBoxList ID=CheckBoxListColour runat=server RepeatColumns= RepeatDirection=Horizontal OnDataBound=CheckBoxListColour_DataBound OnSelectedIndexChanged=CheckBoxListColour_SelectedIndexChanged AutoPostBack=true> </asp:CheckBoxList> 从上面的数据下载并放入专案中然后读出所有图片文件 View Code private List<string> ImageNames { get { List<string> o = new List<string>() DirectoryInfo di = new DirectoryInfo(ServerMapPath (~/Colours)) FileInfo[] fiArray = diGetFiles() for (int i = ; i < fiArrayLength; i++) { oAdd(fiArray[i]Name) } return o; } } 绑定数据至CheckBoxList控件 View Code protected void Page_Load(object sender EventArgs e) { if (!IsPostBack) { Data_Binding() } } private void Data_Binding() { thisCheckBoxListColourDataSource = ImageNamesSelect(c => new { value = c })ToList() thisCheckBoxListColourDataTextField = value; thisCheckBoxListColourDataBind() } CheckBoxList控件的OnDataBound=CheckBoxListColour_DataBound事件 View Code protected void CheckBoxListColour_DataBound(object sender EventArgs e) { var cbl = sender as CheckBoxList; foreach (ListItem li in cblItems) { liText = stringFormat(<img src=Colours/{} /> liValue) } } CheckBoxList控件的OnSelectedIndexChanged=CheckBoxListColour_SelectedIndexChanged事件 View Code protected void CheckBoxListColour_SelectedIndexChanged(object sender EventArgs e) { var cbl = sender as CheckBoxList; foreach (ListItem li in cblItems) { if (liSelected) { liAttributesAdd(style backgroundcolor: red;) } } } |