I have a asp.net checkbox list that is dynamically generated and I need to process some code if the checkbox was initially checked and is unchecked.
This needs to be done in Jquery or Javascript.
All the examples I see deal with getting all values of “Checked” I am only interested in getting the specific item that triggered the onClick() event’s ID so that I can run some other code to process it.
public void DrawPoolWeekCbList()
{
var poolWeeks = ControllerFacade.GetAllPoolWeeksByPoolYear(PageView.PoolWeekYear.Text);
var pWeekCount = poolWeeks.Count;
var curRecord = 0;
foreach (var poolWeek in poolWeeks)
{
PageView.cblPoolWeeks.Items.Add(poolWeek);
PageView.cblPoolWeeks.Items[curRecord].Attributes.Add("onclick", "OnCheckBoxClicked("+ poolWeek +")");
curRecord++;
}
}
//UI Code
function OnCheckBoxClicked(poolWeek) {
var chkId = poolWeek.toString();
$('#<%= lblPoolWeekChange.ClientID %>').text(chkId);
$(".addNew").dialog("open");
}
All the samples I have seen find ALL checked checkboxes and loop through them all, again I just want the SPECIFIC checkboxes id that triggered the function.
Thanks!
//Generated HTML -
<input id="ContentPlaceHolder1_cblPoolWeeks_0" type="checkbox" name="ctl00$ContentPlaceHolder1$cblPoolWeeks$0" onclick="OnCheckBoxClicked(01);" value="01" /><label for="ContentPlaceHolder1_cblPoolWeeks_0">01</label></td><td><input id="ContentPlaceHolder1_cblPoolWeeks_1" type="checkbox" name="ctl00$ContentPlaceHolder1$cblPoolWeeks$1" onclick="OnCheckBoxClicked(02);" value="02" /><label for="ContentPlaceHolder1_cblPoolWeeks_1">02</label></td><td><input id="ContentPlaceHolder1_cblPoolWeeks_2" type="checkbox" name="ctl00$ContentPlaceHolder1$cblPoolWeeks$2" onclick="OnCheckBoxClicked(03);" value="03" /><label for="ContentPlaceHolder1_cblPoolWeeks_2">03</label>
This snippet worked for me, keep in mind there is only one CheckBoxList on the asp.net page, if there was more than one this might not work – Keep in mind this is for handling a SINGLE checkbox and not for iterating through as in “CHECK ALL” functionality. This is handy if you need to add all checked values to a session variable(My case) for a later update of the records.