How can I get selected values ​​of CheckBoxList, something that doesn’t work for me, C # .NET / VisualWebPart

I create a CheckBoxList in the class file and use HTMLTextWriter to render the control.

I use the following code to store selected values ​​in a string:

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

I went through the code and didn't seem to get inside the if statement, and the attribute of the selected value was always false ... Does anyone have an idea how I can solve this?

I fill it using the following:

  YrChkBox.Items.Add(new ListItem("Item 1", "Item1")); 
+10
html c # htmltextwriter
Mar 01 2018-12-12T00:
source share
6 answers

On the aspx page you have a list:

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

In your code behind the aspx.cs page, you have the following:

  protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { // Populate the CheckBoxList items only when it 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)); } 

Make sure you use the if (!IsPostBack) { } condition, because if you load it on every updated page, it actually destroys the data.

+26
Mar 01 2018-12-12T00:
source share

Try something like this:

 foreach (ListItem listItem in YrChkBox.Items) { if (listItem.Selected) { //do some work } else { //do something else } } 
+5
Mar 01 '12 at 20:22
source share

check the box in the list of selected values ​​using the separator

  string items = string.Empty; foreach (ListItem i in CheckBoxList1.Items) { if (i.Selected == true) { items += i.Text + ","; } } Response.Write("selected items"+ items); 
+3
Dec 10 '13 at 9:11
source share

An elegant way to implement this would be to make an extension method, for example:

 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; } } 

Then I can use something like this to compose a string, all values ​​are separated by a ';':

 string.Join(";", cbl.GetSelectedItems()); 
+1
Jun 06 '16 at 12:08 on
source share

//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 value // string YrStrList = ""; foreach (ListItem listItem in ChkMonth.Items) { if (listItem.Selected) { YrStrList = YrStrList + "'" + listItem.Value + "'" + ","; } } sMonthStr = YrStrList.ToString(); 
0
May 29 '14 at 5:50
source share

//aspx.cs

// Load Selected CheckBoxList Items In 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; } } } } 
-one
Jul 21 '14 at 9:14
source share



All Articles