DataBind CheckBoxList with Dictionary
DataBindCheckBoxListWithDictionary.aspx
<%@ Page Language="C#" AutoEventWireup="true" %>
<%@ Import Namespace="System.Collections.Generic" %>
<!DOCTYPE html>
<script runat="server">
protected void Button1_Click(object sender, System.EventArgs e)
{
Dictionary<string, string> colors = new Dictionary<string, string>();
colors.Add("BurlyWood", "#DEB887");
colors.Add("Crimson", "#DC143C");
colors.Add("DarkBlue", "#00008B");
colors.Add("DarkSlateGray", "#2F4F4F");
colors.Add("DarkViolet", "#9400D3");
Label1.Text = "Dictionary Keys...<br />";
foreach (string color in colors.Keys)
{
Label1.Text += color + "<br />";
}
CheckBoxList1.DataSource = colors;
CheckBoxList1.DataTextField = "Key";
CheckBoxList1.DataValueField = "Value";
CheckBoxList1.DataBind();
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>How to DataBind CheckBoxList with Dictionary DataSource</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:Navy; font-style:italic;">
System.Collections.Generic.Dictionary
<br /> How to DataBind CheckBoxList with Dictionary DataSource
</h2>
<hr width="600" align="left" color="Navy" />
<br />
<asp:Label
ID="Label1"
runat="server"
ForeColor="Purple"
Font-Names="Courier New"
Font-Size="Large"
>
</asp:Label>
<br />
<asp:CheckBoxList
ID="CheckBoxList1"
runat="server"
RepeatColumns="2"
ForeColor="DarkGreen"
>
</asp:CheckBoxList>
<br />
<asp:Button
ID="Button1"
runat="server"
OnClick="Button1_Click"
Text="DataBind CheckBoxList"
Height="45"
Font-Bold="true"
ForeColor="DodgerBlue"
/>
</div>
</form>
</body>
</html>
