开发者

asp.net checkboxlist problem, not getting value

开发者 https://www.devze.com 2022-12-23 00:41 出处:网络
Then: protected void Page_Load(object 开发者_开发技巧sender, EventArgs e) { if (!Page.IsPostBack)

Then:

protected void Page_Load(object 开发者_开发技巧sender, EventArgs e)
{
 if (!Page.IsPostBack)
 {
  CheckBoxList1.Items.Add(new ListItem("item1"));
  CheckBoxList1.Items.Add(new ListItem("item2"));
 }
 else
  CheckState();
}

Problem is everytime I debug in CheckState(), CheckBoxList1.Item[0].Selected and CheckBoxList1.Item[1].Selected is always false, even if the checkbox is actually checked!

help?


You need to add the items to your checkbox list in your page initialization code rather than Page_Load.

ASP.NET takes the values users post and maps them to your controls during PreLoad (between Init and Load). If you haven't added the items to your CheckBoxList yet, they don't exist yet, so ASP.NET can't select them.

protected void Page_Init(object sender, EventArgs e) {
    CheckBoxList1.Items.Add(new ListItem("item1"));
    CheckBoxList1.Items.Add(new ListItem("item2"));
}

protected void Page_Load(object sender, EventArgs e) {
    if (!Page.IsPostBack) {
        CheckState();
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号