Hi ,
Please refer below sample.
Note: For this sample i have used temporary DataTable. For more details refer How to create Temporary Table in ASP.Net using C# and VB.Net.
HTML
<asp:CheckBoxList ID="chkCutomers" runat="server">
</asp:CheckBoxList>
Namespaces
C#
using System.Data;
VB.Net
Imports System.Data
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[] {
new DataColumn("Id", typeof(int)),
new DataColumn("Name", typeof(string))
});
dt.Rows.Add(1, "John Hammond");
dt.Rows.Add(2, "Mudassar Khan");
dt.Rows.Add(3, "Suzanne Mathews");
dt.Rows.Add(4, "Robert Schidner");
this.chkCutomers.DataSource = dt;
this.chkCutomers.DataTextField = "Name";
this.chkCutomers.DataValueField = "Id";
this.chkCutomers.DataBind();
string[] ids = new string[] { "1", "4", "3" };
foreach (ListItem item in chkCutomers.Items)
{
if (ids.Contains(item.Value))
{
item.Selected = true;
}
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn() {New DataColumn("Id", GetType(Integer)), New DataColumn("Name", GetType(String))})
dt.Rows.Add(1, "John Hammond")
dt.Rows.Add(2, "Mudassar Khan")
dt.Rows.Add(3, "Suzanne Mathews")
dt.Rows.Add(4, "Robert Schidner")
Me.chkCutomers.DataSource = dt
Me.chkCutomers.DataTextField = "Name"
Me.chkCutomers.DataValueField = "Id"
Me.chkCutomers.DataBind()
Dim ids As String() = New String() {"1", "4", "3"}
For Each item As ListItem In chkCutomers.Items
If ids.Contains(item.Value) Then
item.Selected = True
End If
Next
End If
End Sub
Screenshot