By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
462,049 Members | 670 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 462,049 IT Pros & Developers. It's quick & easy.

Get/Retrieve values of dynamically generated checkboxlist in datalist

P: 6
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();
}
}
}
Nov 7 '07 #1
Share this Question
Share on Google+
1 Reply

P: 6
Hi again,

after a brief struggle with the code and a few readings, I managed to get the right set of code to retrieve the items selected....

"dtlSpecializationCat" is the datalist, I found that I had to reiterate thru the controls within the datalist to retrieve the checked ListItem values.

as all the CheckBoxLists have the ID "chkListSpecializations".. so all I had to do was find that control since I had looped through the controls and the ids in the datalist and the CheckBoxLists all had the same name.

This method is invoked on clicking a register button.

protected void Register_ClickSubmit(object sender, EventArgs e)
{
string specializations = "";
foreach (DataListItem dli in dtlSpecializationCat.Items)
{
CheckBoxList cbL = (CheckBoxList)dli.FindControl("chkListSpecializati ons");
if (cbL != null)
{
foreach (ListItem li in cbL.Items)
{
if (li.Selected)
{
specializations += li.Text + " (" + li.Value + ")<br>";
}
}
}
else
{
specializations += "null :( - bad ";
}
}
litTest.Text += specializations;
}

So there, answered my own question.
Nov 7 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.