准备数据:
http://www.cnblogs.com/insus/articles/1439030.html
.aspx:
<asp:CheckBoxList ID="CheckBoxListColour" runat="server" RepeatColumns="10" RepeatDirection="Horizontal"
OnDataBound="CheckBoxListColour_DataBound"
OnSelectedIndexChanged="CheckBoxListColour_SelectedIndexChanged"
AutoPostBack="true">
</asp:CheckBoxList>
从上面的数据,下载并放入asp.net专案中,然后读出所有图片文件:
private List<string> ImageNames
{
get
{
List<string> o = new List<string>();
DirectoryInfo di = new DirectoryInfo(Server.MapPath ("~/Colours"));
FileInfo[] fiArray = di.GetFiles();
for (int i = 0; i < fiArray.Length; i++)
{
o.Add(fiArray[i].Name);
}
return o;
}
}
绑定数据至CheckBoxList控件:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Data_Binding();
}
}
private void Data_Binding()
{
this.CheckBoxListColour.DataSource = ImageNames.Select(c => new { value = c }).ToList();
this.CheckBoxListColour.DataTextField = "value";
this.CheckBoxListColour.DataBind();
}
CheckBoxList控件的OnDataBound="CheckBoxListColour_DataBound"事件。
protected void CheckBoxListColour_DataBound(object sender, EventArgs e)
{
var cbl = sender as CheckBoxList;
foreach (ListItem li in cbl.Items)
{
li.Text = string.Format("<img src='Colours/{0}' />", li.Value);
}
}
CheckBoxList控件的OnSelectedIndexChanged="CheckBoxListColour_SelectedIndexChanged"事件。
protected void CheckBoxListColour_SelectedIndexChanged(object sender, EventArgs e)
{
var cbl = sender as CheckBoxList;
foreach (ListItem li in cbl.Items)
{
if (li.Selected)
{
li.Attributes.Add("style", "background-color: red;");
}
}
}
高亮CheckBoxList选中的项目
热推产品
- ActiveReport... 强大的.NET报表设计、浏览、打印、转换控件,可以同时用于WindowsForms谀坔攀戀Forms平台下......
- AnyChart AnyChart使你可以创建出绚丽的交互式的Flash和HTML5的图表和仪表控件。可以用于仪表盘的创......
推荐产品