0

I'm generating asp checkbox as:

var chkbx1 = new CheckBox();
chkbx1.ID = "cb" + tableRow[1].ToString();

And I want to run some JavaScript function say myFunction() on change of checkbox state. Is there any way this could be done?

s_anzer
  • 123
  • 1
  • 1
  • 10

2 Answers2

0

Check this answer. It's the same thing but instead of the click event it will be the checked event.

Example:

var chkbx1 = new CheckBox();
chkbx1.ID = "cb" + tableRow[1].ToString();
chkbx1.Checked += (s,e) => { your code; };
container.Controls.Add(button);

Didn't test it's probably what you want. Although, this is server side event. Do you really want a client side event (I mean, it's a server side control, so you might do either)?

taiko
  • 458
  • 6
  • 22
0

If you want to do it from the server, you can add an onclick or onchange event handler via the server:

checkbox.Attributes.Add("onclick", "function(this);");

OR:

checkbox.Attributes.Add("onchange", "function(this);");

Or simply attach to client-side elements on the client using the ID via JavaScript, or by tag via jQuery.

Brian Mains
  • 50,520
  • 35
  • 148
  • 257