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

来源:百度知道 编辑:UC知道 时间:2024/06/20 13:16:15
【代码1】根据读取图片数量动态生成对应的一组CheckBox:

while (i-- != 0 )
{
Image im = new Image();
im.ID = i.ToString();//
it3.writeFile((byte[])Pics[i], "UI\\pic\\Heritage" + i.ToString() + ".jpg");
im.ImageUrl = "~/pic/Heritage" + i.ToString() + ".jpg";

CheckBox cb = new CheckBox();
cb.Text = "选中删除";
cb.Font.Size = System.Web.UI.WebControls.FontUnit.Small;
cb.ID = "cb"+i.ToString();//
cb.Checked = false;

this.Panel1.Controls.Add(im);【动态添加图片】
this.Panel1.Controls.Add(cb);【动态添加checkbox】
this.Panel1.Controls.Add(new LiteralControl("<br>"));
this.Panel1.Controls.Add(new LiteralControl("<br>"));

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("<br>"));
this.p1.Controls.Add(new LiteralControl("<br>"));
}

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

}
}
以上代码通过测试!