Reputation:
I have a array of checkboxes, 33 in total. Because the checkboxes are all over the page, it needs to be in an array (right?).
The code-behind looks like this:
CheckBox[] variableName = new CheckBox[33];
variableName[0] = idCheckBox1;
variableName[1] = idCheckBox1;
variableName[2] = idCheckBox1;
variableName[3] = idCheckBox1;
and so on...
ASP.NET:
<asp:CheckBox ID="idCheckBox1" runat="server" Value="1" />
<asp:CheckBox ID="idCheckBox2" runat="server" Value="2" />
...
The checkboxes are totally optional. Now how do i get the value of the checked boxes only in a variable?
Upvotes: 1
Views: 4483
Reputation: 5325
It does NOT need to be an array.
This is how you get all the CHECKED checkboxes in your form from the CodeBehind:
var names = formCollection.AllKeys.Where(c =>
c.StartsWith("idCheckBox") &&
formCollection.GetValue(c) != null &&
formCollection.GetValue(c).AttemptedValue == "1");
Upvotes: 2