Click here to Skip to main content
15,899,025 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using a checkboxlist inside a repeater control. i am binding the checkbox list like this

<asp:CheckBoxList ID="CheckBoxList1" runat="server" DataSource='<%# ((System.Data.DataRowView)Container.DataItem).Row.GetChildRows("Child") %>' >

but i am not able to use the datatext field and Datavalue field property...How to give this.
Posted

take a class for Property.
C#
public class CheckBoxValues
{
   public string ValueToDisplay{get;set;}
   public string ValueToBind{get;set;}
}

Now Take a list of object of this class and bind this list with your value like...
C#
...
List<checkboxvalues> ChkList =  new List<checkboxvalues>();
//take new object of class.
CheckBoxValues obj;
obj = new CheckBoxValues();
obj.ValueToDisplay = "Cricket";
obj.ValueToBind = "crick";
ChkList.add(obj);
//lets add another object
obj = new CheckBoxValues();
obj.ValueToDisplay = "Football";
obj.ValueToBind = "ft";
ChkList.add(obj);
//you can also add this dynamically.
...
//now give it as your checkBox list's textfield and valuefield.
chklistControll.DataSource = ChkList;
chklistControll.DataTextField = "ValueToDisplay";
chklistControll.DataValueField = "ValueToBind";
chklistControll.DataBind();


Hope This Help
-----------------
Pratik Bhuva
 
Share this answer
 
v3
 
Share this answer
 
ASP.NET
<asp:checkboxlist runat="server" datatextfield="[ColumnName]" datavaluefield="[ColumnName]" />
 
Share this answer
 
v2
Comments
Member 9492907 28-Oct-13 7:18am    
It is not working
CodeBlack 28-Oct-13 7:19am    
what is the error ? and what is the datasource ?
Member 9492907 28-Oct-13 7:22am    
<asp:CheckBoxList ID="CheckBoxList1" runat="server" DataSource='<%# ((System.Data.DataRowView)Container.DataItem).Row.GetChildRows("Child") %>' >

i am nesting the check box list inside a repeater .

i am getting the error like "DataBinding: 'System.Data.DataRow' does not contain a property with the name '[ItemName]'."

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900