PROBLEM
Few days back, my client asked me to add hyperlinks to asp CheckBoxList list-item , so that the user can open a pdf / doc file on click of it and after reading it check the check-boxes based on their requirement. But the thing was that, the CheckBoxList is bind from code-behind, so we can not simply do something like this :-
<asp:CheckBoxList ID="cbl" runat="server"> <asp:ListItem Value="0"><a href="http://www.asp.net">asp.net</a></asp:ListItem> <asp:ListItem Value="1"><a href="http://ui.asp.net">ui.asp.net</a></asp:ListItem> </asp:CheckBoxList>
SOLUTION
So, what I did is :-
ASPX <asp:CheckBoxList ID="cbl" runat="server"> </asp:CheckBoxList> C# DataTable dt = new DataTable(); dt = GetData(); // A methods which returns DataTable if (dt.Rows.Count > 0) { cbl.DataSource = dt; cbl.DataTextField = "DESCRIPTION"; cbl.DataValueField = "PK_ID"; cbl.DataBind(); for (int i = 0; i < dt.Rows.Count; i++) { cbl.Items[i].Text = " <a href='" + dt.Rows[i]["URL"] + "' target='_blank' title='View Document'>" + cbl.Items[i].Text + "</a>"; } }
Happy coding