【有源码】asp.net动态加载一些CheckBox,可是在使用动态加载的checkbox时,却。。

2024-12-22 23:41:53
推荐回答(1个)
回答1:

protected void Page_Load(object sender, EventArgs e)
{
int i = 5;
while(i--!=0){
CheckBox cb = new CheckBox();
cb.Text = "选中删除";
cb.Font.Size = System.Web.UI.WebControls.FontUnit.Small;
cb.ID = "cb" + i.ToString();//
cb.Checked = false;
this.p1.Controls.Add(cb);
this.p1.Controls.Add(new LiteralControl("
"));
this.p1.Controls.Add(new LiteralControl("
"));
}

}
protected void btn1_Click(object sender, EventArgs e)
{
for (int i = 0; i <= 4; i++)//这里从0开始取值,默认!
{
string id = "cb" + i.ToString();
CheckBox cbb = (CheckBox)this.FindControl(id);

cbb.Checked = true;

}
}
以上代码通过测试!