熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

高亮CheckBoxList選中的項目

2013-11-13 10:13:03  來源: .NET編程 
      先看看效果

 \


  准備數據
 
      標准顏色圖片從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;
   
    }
   
    }
   
    }


From:http://tw.wingwit.com/Article/program/net/201311/12949.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.