Hi
I have a datalist which loops through the categories and within the datalist I have a CheckBoxList which are bound on the DataList's OnItemDataBound.
The only problem I have is retrieving the values that have been checked when the user submits the form.
This code works as far as presentation goes but I can't seem to retrieve the checked values in the various checkboxlists that have been generated on the datalist's OnItemDataBound
Below is part of what my page is doing
<asp:DataList ID="dtlSpecializationCat" OnItemDataBound="SpecCat_OnItemDataBound" runat="server" RepeatColumns="4" RepeatDirection="Horizontal">
<ItemTemplate>
<h3 class="specListItemHeader"><%# DataBinder.Eval(Container.DataItem, "CategoryName") %></h3>
<asp:CheckBoxList ID="chkListSpecializations" runat="server">
</asp:CheckBoxList>
</ItemTemplate>
</asp:DataList>
Code Behind for ItemDataBound and binding the DataList
protected void SpecCat_OnItemDataBound(object sender, DataListItemEventArgs e)
{
DataListItem item = e.Item;
if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
{
CheckBoxList checkBox = (CheckBoxList)item.FindControl("chkListSpecializat ions");
System.Data.Common.DbDataRecord record = (System.Data.Common.DbDataRecord)item.DataItem;
int id = 0;
int.TryParse(record["SpecCategoryID"].ToString(),out id);
if (id > 0)
{
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrin gs["conn"].ToString()))
{
using (SqlCommand cmd = new SqlCommand("proc_GetSpecsByCat", conn))
{
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter catIDParam = new SqlParameter("@CatID", SqlDbType.Int);
catIDParam.Value = id;
cmd.Parameters.Add(catIDParam);
conn.Open();
using (SqlDataReader rdr = cmd.ExecuteReader())
{
while (rdr.Read())
{
ListItem li = new ListItem();
li.Value = rdr["SpecializationID"].ToString();
li.Text = rdr["SpecializationName"].ToString();
checkBox.Items.Add(li);
}
}
}
}
}
}
item.Dispose();
}
protected void bindSpecializationCategories()
{
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrin gs["conn"].ToString()))
{
using (SqlCommand cmd = new SqlCommand("proc_GetCategories", conn))
{
cmd.CommandType = CommandType.StoredProcedure;
conn.Open();
dtlSpecializationCat.DataSource = cmd.ExecuteReader();
dtlSpecializationCat.DataBind();
}
}
}