如何动态生成checkbox
来源:百度知道 编辑:UC知道 时间:2024/05/23 19:51:28
CheckBox chk=new CheckBox();
我知道可以用这条命令来动态生成,如何使选项和后台数据库绑定呢
像下面那样做就可以了。
protected void Page_Load(object sender, EventArgs e)
{
CheckBox chk = new CheckBox();
chk.Text = "testall"; // 这里可以换成数据库的内容
chk.CheckedChanged += new EventHandler(chk_CheckedChanged);
chk.AutoPostBack = true;
Page.Form.Controls.Add(chk);
for (int i = 0; i < 10; i++)
{
CheckBox chk2 = new CheckBox();
chk2.Text = "test" + i.ToString(); // 这里可以换成数据库的内容
chk2.Checked = (i % 3 == 0); // 这里可以换成数据库的内容
Page.Form.Controls.Add(chk2);
}
}
void chk_CheckedChanged(object sender, EventArgs e)
{
CheckBox all = sender as CheckBox;
foreach(Control ctl in Page.Form.Controls)
{
if (ctl is CheckBox)
{
CheckBox chk = ctl as Check