I thought that the CheckBoxList databinding would be simlar to DropDownList databinding, and the first part is but I'm not sure where to begin for the "real" databinding.
The items in the CheckBoxList are easily populated from a SqlDataSource, so I have Text for CheckBox1 = "CheckBox1" Value for CheckBox1 = 1, Text for CheckBox2 = "CheckBox2" Value for CheckBox2 = 2 and so on. That of course does not set the Checked state
for any of the CheckBoxes. Asuming that the CheckBoxList is within a FormView and I have a SqlDataSource available that contains two columns, one is a bit which contains the Checked state and the other is a Value which corresponds to the Value from the first
SqlDataSource that was used to populate the list - how do I tell the CheckBoxList to get the Checked state from the second SqlDataSource.
Better yet what if the second data source is not the data source for the FormView that the CheckBoxList is within.
Yes, I know. That gets me the items in the list as I said in my post. What that does not get me is whether a particular item in the list is checked or not.
tdinpsp
Member
114 Points
125 Posts
CheckBoxList DataBinding
Sep 09, 2009 06:47 AM|LINK
I thought that the CheckBoxList databinding would be simlar to DropDownList databinding, and the first part is but I'm not sure where to begin for the "real" databinding.
The items in the CheckBoxList are easily populated from a SqlDataSource, so I have Text for CheckBox1 = "CheckBox1" Value for CheckBox1 = 1, Text for CheckBox2 = "CheckBox2" Value for CheckBox2 = 2 and so on. That of course does not set the Checked state for any of the CheckBoxes. Asuming that the CheckBoxList is within a FormView and I have a SqlDataSource available that contains two columns, one is a bit which contains the Checked state and the other is a Value which corresponds to the Value from the first SqlDataSource that was used to populate the list - how do I tell the CheckBoxList to get the Checked state from the second SqlDataSource.
Better yet what if the second data source is not the data source for the FormView that the CheckBoxList is within.
Thanks
checkboxlist
mamtagupta
Member
328 Points
169 Posts
Re: CheckBoxList DataBinding
Sep 09, 2009 10:24 AM|LINK
Hi tdinpsp,
You have any sql table to bind data with checkboxlist.
Just bind it like dropdown.
CheckBoxList1.DataSource=dt;//datatable
CheckBoxList1.DataTextField="Name";//table field which you want to show
CheckBoxList1.DataValueField="ID";
CheckBoxList1.DataBind();
Regards
Mamta
tdinpsp
Member
114 Points
125 Posts
Re: CheckBoxList DataBinding
Sep 09, 2009 09:41 PM|LINK
Yes, I know. That gets me the items in the list as I said in my post. What that does not get me is whether a particular item in the list is checked or not.
mamtagupta
Member
328 Points
169 Posts
Re: CheckBoxList DataBinding
Sep 10, 2009 07:17 AM|LINK
Hi tdinpsp,
If you want to get checked items try this code
String taxes="";
foreach(ListItem item in checkboxlist1.Items)
{
if (item.Selected)
taxes = taxes + item.Value + ",";
}
Regards
Mamta