【问题标题】:asp:ListItem item to be auto checked in a CheckBoxListasp:ListItem 项在 CheckBoxList 中自动检查
【发布时间】:2019-04-25 11:33:26
【问题描述】:

如果下面的 if 语句为真,如何只自动检查选项 3? 我的 .aspx 中有以下内容:

<asp:CheckBoxList ID="CB_Test" runat="server">
<asp:ListItem Text="Opt 1" Value="1" />
<asp:ListItem Text="Opt 2" Value="2" />
<asp:ListItem Text="Opt 3" Value="3" />
</asp:CheckBoxList>

在.aspx.cs页面中,

if(variable = "3")
CB_Test.Checked = true;

以上对我不起作用。

【问题讨论】:

  • 你在什么情况下这样做>?
  • 在项目数据绑定事件中你应该能够做到这一点

标签: c# asp.net checkboxlist listitem


【解决方案1】:

我想您想检查特定项目取决于值而不是 CheckBoxList 本身。

因此,你想要的应该是这样的

foreach (ListItem item in CB_Test.Items)
{
    if (item.Value == "3")
        item.Selected = true;
}

【讨论】:

    猜你喜欢
    • 2014-08-01
    • 2023-03-24
    • 2011-10-02
    • 1970-01-01
    • 2011-06-05
    • 2023-03-23
    • 2014-05-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多