0

コード bcz の何が問題なのか教えてください。ページにコントロールを追加できません。正しい値を取得しています &CheckBoxList2.Items.Add(row["subj_nme"].ToString());チェックボックスだけが作成された場合

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);
                }
            }
4

1 に答える 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;
}
于 2012-05-06T17:42:43.150 に答える