Bind (Populate) CheckBoxList inside ListView using C# and VB.Net in ASP.Net

Last Reply 8 days ago By dharmendr

Posted 8 days ago

How to Populate Checkboxlist in Listview.

I have trouble repopulaing data to the checkboxlist. 

<asp:ListView ID="ListView1" runat="server" DataKeyNames="ClinicalExpId" DataSourceID="SqlDataSource1" InsertItemPosition="FirstItem">
    <EditItemTemplate>
        <asp:CheckBoxList ID="chkICUType" runat="server" RepeatLayout="Flow">
            <asp:ListItem Value="Medical ICU">Medical ICU</asp:ListItem>
            <asp:ListItem Value="Surgical ICU">Surgical ICU</asp:ListItem>
            <asp:ListItem Value="Coronary Care Unit">Coronary Care Unit</asp:ListItem>
            <asp:ListItem Value="Trauma ICU">Trauma ICU</asp:ListItem>
            <asp:ListItem Value="Cardiovascular ICU">Cardiovascular ICU</asp:ListItem>
            <asp:ListItem Value="Neuro ICU">Neuro ICU</asp:ListItem>
            <asp:ListItem Value="Burn ICU">Burn ICU</asp:ListItem>
            <asp:ListItem Value="CT ICU">CT ICU</asp:ListItem>
            <asp:ListItem Value="" style="display: none;"></asp:ListItem>
        </asp:CheckBoxList>
    </EditItemTemplate>
</asp:ListView>

 

    Protected Sub ListView1_ItemDataBound(sender As Object, e As ListViewItemEventArgs) Handles ListView1.ItemDataBound  
        If e.Item.ItemType = ListViewItemType.DataItem Then
            If Not dataItem.DataItem("ICUType") Is DBNull.Value Then
                Dim items As String() = dataItem.DataItem("ICUType").ToString.Split(",")
                Dim chb As CheckBoxList = DirectCast(e.Item.FindControl("chkICUType"), CheckBoxList)
                For i = 0 To items.GetUpperBound(0)
                    Dim currentCheckBox As ListItem
                    currentCheckBox = chb.Items.FindByValue(items(i).ToString())
                    If currentCheckBox IsNot Nothing Then
                        currentCheckBox.Selected = True
                    End If
                Next
            End If
        End If
    End Sub

 

Posted 8 days ago Modified on 7 days ago

Hi kelsen1989,

Check the below example. Use ItemTemplate.

HTML

<asp:ListView ID="ListView1" runat="server" DataKeyNames="Id" OnItemDataBound="ListView1_ItemDataBound">
    <ItemTemplate>
        <asp:Label Text='<%#Eval("Name") %>' runat="server" />
        <hr />
        <asp:CheckBoxList ID="chkICUType" runat="server" RepeatLayout="Flow">
            <asp:ListItem Value="Medical ICU">Medical ICU</asp:ListItem>
            <asp:ListItem Value="Surgical ICU">Surgical ICU</asp:ListItem>
            <asp:ListItem Value="Coronary Care Unit">Coronary Care Unit</asp:ListItem>
            <asp:ListItem Value="Trauma ICU">Trauma ICU</asp:ListItem>
            <asp:ListItem Value="Cardiovascular ICU">Cardiovascular ICU</asp:ListItem>
            <asp:ListItem Value="Neuro ICU">Neuro ICU</asp:ListItem>
            <asp:ListItem Value="Burn ICU">Burn ICU</asp:ListItem>
            <asp:ListItem Value="CT ICU">CT ICU</asp:ListItem>
        </asp:CheckBoxList>
        <br /><br />
    </ItemTemplate>
</asp:ListView>

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        System.Data.DataTable dt = new System.Data.DataTable();
        dt.Columns.AddRange(new System.Data.DataColumn[3] {
        new System.Data.DataColumn("Id"),
        new System.Data.DataColumn("Name"),
        new System.Data.DataColumn("ICUType") });
        dt.Rows.Add(1, "John Hammond", "Medical ICU,Neuro ICU");
        dt.Rows.Add(2, "Mudassar Khan", "Surgical ICU,Medical ICU");
        dt.Rows.Add(3, "Suzanne Mathews", "Trauma ICU");
        dt.Rows.Add(4, "Robert Schidner", "CT ICU");
        ListView1.DataSource = dt;
        ListView1.DataBind();
    }
}

protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
{
    if (e.Item.ItemType == ListViewItemType.DataItem)
    {
        ListViewDataItem dataItem = (ListViewDataItem)e.Item;
        string iCUTypes = ((System.Data.DataRowView)dataItem.DataItem).Row["ICUType"].ToString();
        foreach (string iCUType in iCUTypes.Split(','))
        {
            CheckBoxList chb = (CheckBoxList)e.Item.FindControl("chkICUType");
            foreach (ListItem item in chb.Items)
            {if(item.Value.ToLower().Trim() == iCUType.ToLower().Trim())
                {
                    item.Selected = true;
                }
            }
        }
    }
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not IsPostBack Then
        Dim dt As System.Data.DataTable = New System.Data.DataTable()
        dt.Columns.AddRange(New System.Data.DataColumn(2) {
                                New System.Data.DataColumn("Id"),
                                New System.Data.DataColumn("Name"),
                                New System.Data.DataColumn("ICUType")})
        dt.Rows.Add(1, "John Hammond", "Medical ICU,Neuro ICU")
        dt.Rows.Add(2, "Mudassar Khan", "Surgical ICU,Medical ICU")
        dt.Rows.Add(3, "Suzanne Mathews", "Trauma ICU")
        dt.Rows.Add(4, "Robert Schidner", "CT ICU")
        ListView1.DataSource = dt
        ListView1.DataBind()
    End If
End Sub

Protected Sub ListView1_ItemDataBound(ByVal sender As Object, ByVal e As ListViewItemEventArgs)
    If e.Item.ItemType = ListViewItemType.DataItem Then
        Dim dataItem As ListViewDataItem = CType(e.Item, ListViewDataItem)
        Dim iCUTypes As String = (CType(dataItem.DataItem, System.Data.DataRowView)).Row("ICUType").ToString()
        For Each iCUType As String In iCUTypes.Split(","c)
            Dim chb As CheckBoxList = CType(e.Item.FindControl("chkICUType"), CheckBoxList)
            For Each item As ListItem In chb.Items
                If item.Value.ToLower().Trim() = iCUType.ToLower().Trim() Then
                    item.Selected = True
                End If
            Next
        Next
    End If
End Sub