How to display checkbox in a DataList and highlight a row if the value of checkbox is true

Platform: ASP.NET| Category: DataList

<asp:datalist id='DataList1' OnItemDataBound ='ItemDB' runat='server'>
<ItemTemplate >
<asp:CheckBox id='chkDiscontinued' Runat =server  
	checked=<%#Convert.ToBoolean(DataBinder.Eval(Container.DataItem , 'Discontinued').ToString())%>>
</asp:CheckBox> 
<asp:label ID='lblName' runat=server text=<%#DataBinder.Eval(Container.DataItem , 'ProductName').ToString() %>></asp:label>
</ItemTemplate>
</asp:datalist>

VB.NET


’Populate the DataList in the Page_Load Event
Protected Sub ItemDB(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs)
        If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
            If CType(e.Item.FindControl('chkDiscontinued'), CheckBox).Checked Then
                e.Item.BackColor = Color.CadetBlue
            End If
        End If
End Sub

C#


//Bind the DataList in the Page_Load Event
protected void ItemDB(object sender   , System.Web.UI.WebControls.DataListItemEventArgs   e  )
{
	if(( e.Item.ItemType == ListItemType.Item)||( e.Item.ItemType == ListItemType.AlternatingItem) )
	{
		if( ((CheckBox)(e.Item.FindControl('chkDiscontinued'))).Checked  )
		{
			e.Item.BackColor = Color.CadetBlue;
		}
	}   
}	

Share with

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.