469,141 Members | 1,172 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,141 developers. It's quick & easy.

Get/Retrieve values of dynamically generated checkboxlist in datalist

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
1 7653
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.

Similar topics

3 posts views Thread by Ed West | last post: by
4 posts views Thread by krzysiek | last post: by
2 posts views Thread by Patrick.O.Ige | last post: by
4 posts views Thread by Patrick.O.Ige | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Mortomer39 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.