How to attach onchange event to CheckBoxList? Missing InputAttributes?
-
21-09-2019 - |
Question
Setting onchange event for CheckBoxList using the following code doesn't work.
chkListUserGroup.Attributes.Add("onchange", "document.forms[0].isRecordModified.value='true';");
How to set onchange event for CheckBoxList?
Solution
Use onclick event,
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
CheckBoxList1.Items.Add("A");
CheckBoxList1.Items.Add("B");
CheckBoxList1.Items.Add("C");
CheckBoxList1.Items.Add("D");
foreach (ListItem item in CheckBoxList1.Items)
{
item.Attributes.Add("onclick", "document.forms[0].isRecordModified.value=document.activeElement.checked");
}
}
}
OTHER TIPS
Use this code to handle the events of click in Check box list in vb.net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
Dim li As ListItem
For Each li In CheckboxList1.Items
li.Attributes.Add("onclick", "alert('hello')")
Next
End If
Well actually it should be working. Because I write something in my code and it worked. It seems you need to check your javascript code by simply changing it with alert('hello');
foreach (ListItem item in CheckBoxList1.Items)
{
item.Attributes.Add("onchange", "alert('hello')");
}
This is my simple code and it is working.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow