Last post Mar 22, 2012 08:07 PM by DarthSwian
Mar 22, 2012 07:23 PM|Wozer|LINK
I have a save button that I want to loop through all the Checkboxes in a Panel, and update a database value based on if it is checked or not.
Written on the page the checkboxes look like this:
<asp:checkbox name="chkquest" value="3" style="display:none;" checked="yes"></asp:checkbox>
Where the value of the checkbox is the ID of the item being updated.
I know the code needs to be something like this, but I cant figure out how to get the value of the checkbox:
foreach(Control ctrl in pnlQList)
if(ctrl is CheckBox)
//Get if checked or not
Any help with this would be great.
Mar 22, 2012 07:27 PM|DarthSwian|LINK
.net checkboxes don't have a value attribute only checked which is true/false/ Why not just name the checkboxes specifically?
Mar 22, 2012 07:31 PM|Wozer|LINK
<asp:Checkbox ID='"+rd.GetInt32(0)+"' style='display:none;' checked='yes'></asp:Checkbox>";
And then in the for each loop I would do
int IDVal = ctrl.ID
Mar 22, 2012 07:39 PM|DarthSwian|LINK
Are you dynamically adding these textboxes?
Mar 22, 2012 07:42 PM|Wozer|LINK
I am getting a list of items from a database, looping through the reader and adding a checkbox for each item into an <li>
Mar 22, 2012 07:52 PM|DarthSwian|LINK
if you're doing it in a repeated put a hiddenfield with each checkbox that has the value you want and if the checkbox is checked, use that value.
Mar 22, 2012 07:58 PM|Wozer|LINK
I just realized the code for the checkbox should actually look like this:
"<input type='checkbox' name='chkquest' value='"+rd.GetInt32(0)+"' style='display:none;' />"
I do not have it in a repeaters, Just looping through and writing to a <ul> and then outputting it with .InnerHtml
If it instead looked like this:
"<input type='checkbox' ID='"+rd.GetInt32(0)+"' style='display:none;' />"
would my idea of doing
int idval = ctrl.ID
If I did it via a hidden field, how would I get the value of the Hidden Field that is assocated with the correct checkbox?
Mar 22, 2012 07:59 PM|Specs|LINK
foreach (Control ctrl in pageContentPanel.Controls)
if (ctrl.GetType() == typeof(CheckBox))
var checkBoxCtrl = (CheckBox)ctrl;
Mar 22, 2012 08:07 PM|DarthSwian|LINK
Like I said, value isn't a property of asp:checkbox, and I wasn't suggesting you dynamically add the id. If you're getting the textbox from a DB it should have something associated with it that can be added in the text property and accessed that way,