C# Checkboxlist

2019-08-30 08:27发布

Ok so I have a checkbox list with n checkboxes. Everytime I check a box I want that number to get added to a list (Chosen). When the box is unchecked I want to remove it from the list. Then when I hit a button I want to display the list in a textbox. Here is my code:

public partial class _Default : System.Web.UI.Page
{
    List<int> Chosen = new List<int>();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["Chosen"] == null)
        {
            Session["Chosen"] = new List<int>();
        } 

        Chosen = (List<int>)Session["Chosen"];

        for (int i = 0; i < Numbers.Items.Count; i++)
        {
            if (Numbers.Items[i].Selected) { Chosen.Add(i + 1); }
            else { Chosen.Remove(i + 1); }
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        foreach (var i in Chosen)
        {
            TextBox1.Text = Chosen[i].ToString();
        }
    }
}

Needless to say it does not work the way I want it to. When I select 1 and hit the button it writes 1 into the box, but if I unselect it and hit the button it leaves 1 there. Also if I select any number other than 1 I get an error on this line:

TextBox1.Text = Chosen[i].ToString();

Saying index is out of range. Yet when I debug the numbers seem to be allocating to the list properly.

4条回答
Animai°情兽
2楼-- · 2019-08-30 09:19

Try using:

TextBox1.Text = i.ToString();

Also, this will only show the last item selected. If you have multiples selected, you will have to concatenate all and then assign to TextBox1.Text. Or use this:

TextBox1.Text += i.ToString() + " ";
查看更多
劫难
3楼-- · 2019-08-30 09:19

Instead of using TextBox1.Text = Chosen[i].ToString(); use

TextBox1.Text = String.Empty;
foreach (var i in Chosen)
{
   TextBox1.Text += i.ToString();
}

The problem is that when you use foreach, the value of i is actually a value of the actual element of the Chosen list, not an index.

The way you wrote is working well in a for loop:

    for (int i = 0; i < Chosen.Count; i++)
    {
        TextBox1.Text += Chosen[i].ToString();
    }
查看更多
女痞
4楼-- · 2019-08-30 09:27

Is this what you need?

<asp:TextBox ID="textBox1" runat="server">
</asp:TextBox>
<asp:CheckBoxList ID="Numbers" runat="server" 
                   OnSelectedIndexChanged="UpdateCheckBoxex" AutoPostBack="true">
    <asp:ListItem Text="One" Value="One" />
    <asp:ListItem Text="Two" Value="Two" />
    <asp:ListItem Text="Three" Value="Three" />
    <asp:ListItem Text="Four" Value="Four" />
</asp:CheckBoxList>


protected void UpdateCheckBoxex(object sender, EventArgs e)
{
    string s = string.Empty;
    new List<ListItem>(Numbers.Items.Cast<ListItem>().Where(i => i.Selected)).ForEach(x => s += string.Format("{0}, ", x.Text));
    textBox1.Text = s;        
}

In this case you don't need Chosen, but if you still need to populate it, you dont need foreach loop

List<int> Chosen = new List<int>();
Chosen.AddRange(Numbers.Items.Cast<ListItem>().Where(i => i.Selected).Select(x => Numbers.Items.IndexOf(x) + 1));
查看更多
霸刀☆藐视天下
5楼-- · 2019-08-30 09:29

Suppose this is your CheckBoxList

<asp:CheckBoxList ID="Numbers" runat="server" AutoPostBack="True" 
        onselectedindexchanged="Numbers_SelectedIndexChanged">
        <asp:ListItem>1</asp:ListItem>
        <asp:ListItem>2</asp:ListItem>
        <asp:ListItem>3</asp:ListItem>
        <asp:ListItem>4</asp:ListItem>
        <asp:ListItem>5</asp:ListItem>
    </asp:CheckBoxList>

The Code behind event: Edited

 protected void Numbers_SelectedIndexChanged(object sender, EventArgs e)
{
    List<int> Chosen = new List<int>();
  if (Session["Chosen"] == null)
    {
        Session["Chosen"] = new List<int>();
    } 
    Chosen = (List<int>)Session["Chosen"];


    foreach (ListItem it in Numbers.Items)
        if (it.Selected)
        {
            if (!Chosen.Contains(Convert.ToInt32(it.Text)))
                Chosen.Add(Convert.ToInt32(it.Text));
        }
        else if (Chosen.Contains(Convert.ToInt32(it.Text)))
                Chosen.Remove(Convert.ToInt32(it.Text));
    TextBox5.Text="";
    foreach (int n in Chosen)
        TextBox5.Text +=  n.ToString() + " , ";

    Session["Chosen"] = Chosen;

  }

This does what you have explained as per my understanding

Edited and Tested

查看更多
登录 后发表回答