【问题标题】:checkboxlist items as checked by default in codebehind asp.net在 asp.net 代码隐藏中默认选中的复选框列表项
【发布时间】:2014-09-18 15:29:26
【问题描述】:

在我的页面中,我有一个 CheckBoxList 控件,上面有 7 个项目。我想在我的Page_load codebihind 中设置这 7 个项目。

我的页面:

<asp:CheckBoxList ID="WeeklyCondition" runat="server">
    <asp:ListItem Value="1">Sat</asp:ListItem>
    <asp:ListItem Value="2">Sun</asp:ListItem>
    <asp:ListItem Value="3">Mon</asp:ListItem>
    <asp:ListItem Value="4">Tue</asp:ListItem>
    <asp:ListItem Value="5">Wed</asp:ListItem>
    <asp:ListItem Value="6">Thu</asp:ListItem>
    <asp:ListItem Value="7">Fri</asp:ListItem>

</asp:CheckBoxList>

【问题讨论】:

    标签: c# asp.net .net code-behind checkboxlist


    【解决方案1】:

    您可以使用循环迭代通过CheckBoxList的项目集合并更改Selected属性。

    foreach (ListItem item in WeeklyCondition.Items) 
        item.Selected = true;
    

    【讨论】:

      【解决方案2】:

      如果你想检查其中一些条件,你可以使用这样的东西:

      protected void Page_Load(object sender, EventArgs e)
      {
          for (int i = 0; i < CheckBoxList1.Items.Count; i++)
          {
              if(someCondition)
                 CheckBoxList1.Items[i].Selected = true;
          }
      }
      

      来自here

      【讨论】:

      • 更好的是,在 checkboxlist 的 DataBound 事件中进行。
      【解决方案3】:

      如何将复选框列表项设置为默认选中

      第一种方式:

      <asp:CheckBoxList runat="server" ID="CheckBoxList1">
          <asp:ListItem Selected="True">Item1</asp:ListItem>
          <asp:ListItem Selected="True">Item2</asp:ListItem>
          <asp:ListItem Selected="True">Item3</asp:ListItem>
          <asp:ListItem Selected="True">Item4</asp:ListItem>
          <asp:ListItem Selected="True">Item5</asp:ListItem>
      </asp:CheckBoxList>
      

      第二种方式:

      页面文件:

      <asp:CheckBoxList runat="server" ID="CheckBoxList">
          <asp:ListItem>Item1</asp:ListItem>
          <asp:ListItem>Item2</asp:ListItem>
          <asp:ListItem>Item3</asp:ListItem>
          <asp:ListItem>Item4</asp:ListItem>
          <asp:ListItem>Item5</asp:ListItem>
      </asp:CheckBoxList>
      

      代码隐藏:

      protected void Page_Load(object sender, EventArgs e)
      {
          for (int i = 0; i < CheckBoxList.Items.Count; i++)
          {
              CheckBoxList.Items[i].Selected = true;
          }
      }
      

      【讨论】:

        【解决方案4】:
        <asp:ListItem Selected="True">Item1</asp:ListItem>
        

        How can I set checkboxlist items as checked by default

        【讨论】:

        • OK 我正在加载一个简单的表,其中的值都是默认选中的。要读取的循环如下。我认为您想要的关键部分是: cblAnalyteGroup.Items.Add(dRow.Item("AnalyteGroupName").Selected) 页面的代码非常有限: 对于 cblAnalyteGroup.DataSource.Rows 中的每个 dRow 作为 DataRow cblAnalyteGroup.Items.Add(dRow.Item("AnalyteGroupName").Selected) Next
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-15
        • 1970-01-01
        • 2017-05-29
        • 2015-09-19
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多