0

plz tell me whats wrong in code bcz i m not able to add control on page. i m getting the values right & if i just CheckBoxList2.Items.Add(row["subj_nme"].ToString()); the check box get created

if (ds.Tables.Count > 0)
            {
                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    chkList1 = new CheckBox();
                    chkList1.Text = row["subj_nme"].ToString();
                    chkList1.ID = row["subjid"].ToString();
                    chkList1.Checked = true;
                    chkList1.Font.Name = "Verdana";
                    chkList1.Font.Size = 12;
                    CheckBoxList2.Controls.Add(chkList1);
                }
            }
1
  • in which event are you creating this? Commented May 6, 2012 at 17:43

1 Answer 1

2

I think you can use this code for binding your CheckBoxList2 to DataTable as follow.

CheckBoxList2.DataSource = ds.Tables[0];
CheckBoxList2.DataTextField = "subj_nme";
CheckBoxList2.DataValueField = "subjid";
CheckBoxList2.DataBind();
CheckBoxList2.Font.Name = "Verdana";
CheckBoxList2.Font.Size = 12;

To check them all you can do this

for(int i=0;i<CheckBoxList2.Items.Count;i++)
{
CheckBoxList2.Items[i].Selected = true;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.