Determining which items are selected in a CheckBoxList using Request.Form

Using the approach shown in the here question, I can get the selected item values ​​from my CheckBoxList :

 var selectedCheckBoxItems = from key in Request.Form.AllKeys where key.Contains(cbl.ID) select Request.Form.Get(key); 

Then I can iterate over the results:

 foreach (var item in selectedCheckBoxItems) { } 

The problem is that item is just the published value, which for the checkbox is just the string "on".

<w1061 "

I need to determine which element is "on", either by index or by some other method.

Question: How to determine which items in a CheckBoxList are selected using Request.Form ?

Here is my CheckBoxList definition:

 <asp:CheckBoxList runat="server" ID="cblAnimalType" SelectionMode="Multiple" DataTextField="OptionText" DataValueField="OptionId" AutoPostBack="True"/> 

Items are added to the list from the code behind:

 DataTable dt = GetData(SqlGetListOptions, paramList); cbl.DataSource = dt; cbl.DataBind(); 

Another important one is ViewStateMode="Disabled" , so I have to use Request.Form to get the selected items.


In response to the comment, here is what the HTML looks like for CheckBoxList:

html


@Leopard noted that he sees values ​​displayed in HTML that are not found in my situation. AdamE's answer to this question explains why. I have the following line in web.config:

 <pages validateRequest="false" controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID"> 

which explains why I see "on" instead of the actual value of the selected elements. I can’t just pull out the compatibility with web.config without checking that it won’t break something else, but it seems that if this option is safe for deletion, the values ​​of the checkbox list will be accessible from the code.

+5
source share
4 answers

As you bind the checkboxList from the Database using OptionId feild, you can check which checkboxes are selected and save their value in List<int> if their value is int .

Then again, when you fill out the checkboxList , you can check what value present in the list stored earlier and based on value , you can select corresponding checkbox .

Here is a sample code

 List<int> SelectedCheckBoxes = new List<int>(); var selectedCheckBoxItems = from key in Request.Form.AllKeys where key.Contains(cblAnimalType.ID) select Request.Form.Get(key); foreach (var item in selectedCheckBoxItems) { SelectedCheckBoxes.Add(int.Parse(item.ToString())); } 

Then again, when you populate checkboxList , you can find the items on value and select them

 foreach (ListItem listItem in cblAnimalType.Items) { if (SelectedCheckBoxes.Contains((int.Parse(listItem.Value)))) { listItem.Selected = true; } } 

You may need to store the SelectedCheckBoxes List in session depending on how you populate the checkboxList .

Update

According to the discussion with you, you used Asp.net 4.0 , but your checkboxes did not have a value attribute that should have been there.

This is because you are using controlRenderingCompatibilityVersion="3.5" , which is no longer needed because you are already using Asp.net 4.0 .

So removing this line from web.congif will solve the problem and you can get the value of checkbox instead of just getting on

 <pages controlRenderingCompatibilityVersion="3.5" /> 
+1
source

I thought of one way to do this by adding a key to the results. I'm not quite happy with this method due to the parsing of the string needed to get the index. Maybe someone has a better method?

The general idea is to add a key to the results:

 var selectedCheckBoxItems = from key in Request.Form.AllKeys where key.Contains(cbl.ID) select new {Value = Request.Form.Get(key), Key = key}; foreach (var item in selectedCheckBoxItems) { var val = item.Value; var key = item.Key; } 

Then I can parse the key to find the index I need to set the selected options:

 var selectedCheckBoxItems = from key in Request.Form.AllKeys where key.Contains(cbl.ID) select new {Value = Request.Form.Get(key), Key = key}; string[] stringParse = new string[] {listName.Replace(" ", "") + '$'}; foreach (var item in selectedCheckBoxItems) { var val = item.Value; var key = item.Key; var idxArray = key.Split(stringParse, StringSplitOptions.None); var idxString = idxArray[idxArray.Length - 1]; int idxInt; if (Int32.TryParse(idxString, out idxInt)) { cbl.Items[idxInt].Selected = true; } } 
+2
source

Here's another way to parse a key to get the selected indexes. It also uses the UniqueID property to find matching keys.

 var selectedCheckBoxItems = from key in Request.Form.AllKeys where key.Contains(cblAnimalType.UniqueID) select new { Value = Request.Form.Get(key), Key = key }; foreach (var item in selectedCheckBoxItems) { var val = item.Value; string indexToken = item.Key.Replace(cblAnimalType.UniqueID, ""); int index = int.Parse(Regex.Replace(indexToken, @"[^\d]", "")); ... } 
+1
source

I understand that this is very close to the previous answers, so I'm not sure if it adds anything, but it is a bit more concise, so I will add it. In Request.Form.AllKeys , Key is the html name of this flag. The html name of the checkbox contains the index. I would just modify your linq query to select only those elements that are "on" and then return a list of keys. After that, you can simply parse the keys to get the index.

 var selectedCheckBoxItems = from key in Request.Form.AllKeys where key.Contains(cbl.ID) && Request.Form.Get(key) == "on" select key; foreach (var item in selectedCheckBoxItems) { var index = 0; if (int.TryParse(item.Substring(item.LastIndexOf("$") + 1), out index)) { //do what you will } } 

In addition, the parsing in this instance is pretty safe, because the field names are generated by the framework in a very specific and formulaic way. The likelihood that they have ever been different is close to zero, if not zero.

0
source

All Articles