DataList and CheckBoxList don't seem to play well together

D

Dave Loynd

Greetings all-

I am attempting to use a DataList with an embedded and databound
CheckBoxList control. As one might expect, I can add ListItems to the
CheckBoxList, but in no way have I been able to set or retrieve the Selected
property of any of these ListItems.

To perform the data binding, I hook into the ItemCreated event of the
DataList control and when it goes to create an EditItemTemplate I make the
necessary calls to perform the binding and set the Selected state of each
ListItem. Unfortunately, once the HTML is picked up by the client browser
nothing is checked and on postback anything that was checked on the browser
does not show up.

Is there some interplay between the DataList control and embedded
CheckBoxList controls I'm not aware of?

DL
 
A

Alvin Bruney [MVP]

It depends on how you added it. If it's a dynamic control that you added in
from the itemdatabound, you can pick up the selected value using
Request.Form passing in the groupname of the checkboxlist value. FYI: You
would need to specify a group name when you added the control.
 
D

Dave Loynd

In my case, I'm hooking into the ItemCreated event for the DataList and if it's an EditItem that's being created, I take the existing CheckBoxList and do some binding and the the check state:

if(e.Item.ItemType == ListItemType.EditItem)
{
CheckBoxList cblGroups = (CheckBoxList)e.Item.FindControl("cblGroups");
if(cblGroups != null)
{
cblGroups.Visible = true;
cblGroups.DataSource = dsServiceLevel;
cblGroups.DataMember = "Groups";
cblGroups.DataTextField = "description";
cblGroups.DataValueField = "iden";
cblGroups.DataBind();

foreach(ListItem li in cblGroups.Items)
li.Selected = true; // Just checking them all for testing
}
}

Once this bit of code runs, I see all of the individual items I want in the CheckBoxList, but none of them are selected!



----- Alvin Bruney [MVP] wrote: -----

It depends on how you added it. If it's a dynamic control that you added in
from the itemdatabound, you can pick up the selected value using
Request.Form passing in the groupname of the checkboxlist value. FYI: You
would need to specify a group name when you added the control.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top