如何确定复选框列表已选中/选中 -
当我执行代码时,我得到 4 个复选框,并且我选中/选择了所有 4 个复选框,当我尝试调试代码时,它确实算作我有 4 个复选框,但所有 4 个复选框都被选中= false。
我在代码中缺少什么?
<asp:checkboxlist id="chk" runat="server" ondatabinding="chk_DataBinding"
ondatabound="chk_DataBound">
</asp:checkboxlist>
List<String> roles = new List<string>();
for (int i = 0; i < chk.Items.Count; i++)
{
if (chk.Items[i].Selected)
{
roles.Add(chk.Items[i].Value);
}
}
when i execute the code i get 4 checkboxs and i checked/selected all 4 checkbox and when i try to debug the code, it does count that i have 4 checkbox but all 4 checkbox is selected=false.
what i am missing in code?
<asp:checkboxlist id="chk" runat="server" ondatabinding="chk_DataBinding"
ondatabound="chk_DataBound">
</asp:checkboxlist>
List<String> roles = new List<string>();
for (int i = 0; i < chk.Items.Count; i++)
{
if (chk.Items[i].Selected)
{
roles.Add(chk.Items[i].Value);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的逻辑与
ListControl.Items
页面,并根据个人经验,检查ListItem
.Selected 属性代码> 应该可以正常工作。在点击“如果选中”逻辑之前,请检查以确保您没有重新填充
CheckBoxList
- 如果我不得不猜测,我会说您很有可能会丢失该列表在每次回发时。简单的解决方案是,如果是回发,则不要调用数据绑定逻辑。Your logic is consistent with the basic
CheckBoxList
given on theListControl.Items
page, and from personal experience, checking the.Selected
property of theListItem
should work fine.Check to make sure you aren't re-populating the
CheckBoxList
before you hit the "if checked" logic - if I had to guess, I'd say there's a good chance you're losing the list on every postback. The simple solution is don't call your databinding logic if it's a postback.