【问题标题】:How can I get the CheckBoxList selected values, what I have doesn't seem to work C#.NET/VisualWebPart如何获取 CheckBoxList 选择的值,我所拥有的似乎不起作用 C#.NET/VisualWebPart
【发布时间】:2012-03-20 08:44:09
【问题描述】:

我在类文件中创建一个 CheckBoxList 并使用 HTMLTextWriter 来呈现控件。

我正在使用以下代码将选定的值存储在字符串中:

string YrStr = "";
for (int i = 0; i < YrChkBox.Items.Count; i++)
{
    if (YrChkBox.Items[i].Selected)
    {
        YrStr += YrChkBox.Items[i].Value + ";"; 
    }
}

我单步执行了代码,但它似乎并没有触及 if 语句的内部,并且每次选择的 value 属性都是 false ......有人知道我该如何解决这个问题吗?

我使用以下填充它:

 YrChkBox.Items.Add(new ListItem("Item 1", "Item1"));

【问题讨论】:

  • 这段代码应该可以工作..你把这段代码放在什么事件下也你实际上 YrChkBox.Items.Count 有一个值..??
  • 点击; if 语句的答案似乎总是错误的。虽然这是在 Class 文件中,但 PostBacks 与此有什么关系吗?

标签: c# asp.net html htmltextwriter


【解决方案1】:

在您的 ASPX 页面中,您有如下列表:

    <asp:CheckBoxList ID="YrChkBox" runat="server" 
        onselectedindexchanged="YrChkBox_SelectedIndexChanged"></asp:CheckBoxList>
    <asp:Button ID="button" runat="server" Text="Submit" />

在你的 aspx.cs 页面后面的代码中,你有这个:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            // Populate the CheckBoxList items only when it's not a postback.
            YrChkBox.Items.Add(new ListItem("Item 1", "Item1"));
            YrChkBox.Items.Add(new ListItem("Item 2", "Item2"));
        }
    }

    protected void YrChkBox_SelectedIndexChanged(object sender, EventArgs e)
    {
        // Create the list to store.
        List<String> YrStrList = new List<string>();
        // Loop through each item.
        foreach (ListItem item in YrChkBox.Items)
        {
            if (item.Selected)
            {
                // If the item is selected, add the value to the list.
                YrStrList.Add(item.Value);
            }
            else
            {
                // Item is not selected, do something else.
            }
        }
        // Join the string together using the ; delimiter.
        String YrStr = String.Join(";", YrStrList.ToArray());

        // Write to the page the value.
        Response.Write(String.Concat("Selected Items: ", YrStr));
    }

确保您使用if (!IsPostBack) { } 条件,因为如果您在每次页面刷新时加载它,它实际上会破坏数据。

【讨论】:

  • 我在标记中根本没有它。我有它在一个文本作家。请稍等,我将粘贴我的代码。
  • 啊,我明白你在说什么,将选定的值添加到列表中,因为它们被选中...我会试试的,我担心每次用户选择时都会重新加载页面复选框虽然
  • 您可以从 ASPX 页面中删除 AutoPostBack="True" 部分,它在来自另一个事件(例如按下按钮)的回发事件上的行为方式应该相同。这只是一个例子。
  • 我似乎无法在 Code-behind 中添加服务器端控件并通过 HTMLTextWriter 添加呈现它......关于如何做到这一点的任何想法。我真的需要能够检索复选框列表的选定值,谢谢您的帮助+1
【解决方案2】:

试试这样的:

foreach (ListItem listItem in YrChkBox.Items)
{
    if (listItem.Selected)
    { 
       //do some work 
    }
    else 
    { 
      //do something else 
    }
}

【讨论】:

  • @Kraze 正是我现在所拥有的......除了我在 for 循环中使用它,我用你的语法尝试了它,是的,它实际上是一样的,但我更喜欢你的语法,所以我会一直保持哈哈+1
  • 啊,我明白你在说什么,是的,你是对的,我没有那样做。尽管 T_T 我遇到了同样的问题,但我真的希望这能有所帮助。我认为这是因为我的按钮不是服务器端
【解决方案3】:

复选框列出带有分隔符的选定值

 string items = string.Empty;
        foreach (ListItem i in CheckBoxList1.Items)
        {
            if (i.Selected == true)
            {
                items += i.Text + ",";
            }
        }
        Response.Write("selected items"+ items);

【讨论】:

    【解决方案4】:

    实现这一点的一种优雅方法是创建一个扩展方法,如下所示:

    public static class Extensions
    {
        public static List<string> GetSelectedItems(this CheckBoxList cbl)
        {
            var result = new List<string>();
    
            foreach (ListItem item in cbl.Items)
                if (item.Selected)
                    result.Add(item.Value);
    
            return result;
        }
    }
    

    然后我可以使用这样的东西来组成一个字符串,所有值都用';'分隔:

    string.Join(";", cbl.GetSelectedItems());
    

    【讨论】:

      【解决方案5】:

      // Page.aspx //

      // To count checklist item
      
        int a = ChkMonth.Items.Count;
              int count = 0;
      
              for (var i = 0; i < a; i++)
              {
                  if (ChkMonth.Items[i].Selected == true)
                  {
                      count++;
                  }
              }
      

      // Page.aspx.cs //

        // To access checkbox list item's value //
         string YrStrList = "";
              foreach (ListItem listItem in ChkMonth.Items)
              {
                  if (listItem.Selected)
                  {
                      YrStrList = YrStrList + "'" + listItem.Value + "'" + ",";
                  }
      
              }
      
              sMonthStr = YrStrList.ToString();
      

      【讨论】:

        【解决方案6】:

        // aspx.cs

        //将CheckBoxList选中的项加载到ListBox中

            int status = 1;
            foreach (ListItem  s in chklstStates.Items  )
            {
                if (s.Selected == true)
                {
                    if (ListBox1.Items.Count == 0)
                    {
                        ListBox1.Items.Add(s.Text);
        
                    }
                    else
                    {
                        foreach (ListItem list in ListBox1.Items)
                        {
                            if (list.Text == s.Text)
                            {
                                status = status * 0;
        
                            }
                            else
                            {
                                status = status * 1;
                            }
                        }
                        if (status == 0)
                        { }
                       else
                        {
                            ListBox1.Items.Add(s.Text);
                        }
                        status = 1;
                    }
                }
            }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2022-12-11
          • 1970-01-01
          • 2013-12-03
          • 1970-01-01
          • 1970-01-01
          • 2018-06-13
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多