Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

asp.net mvc: simulating autopostback for simple checkbox

Tags:

asp.net-mvc

I have a simple checkbox, generated with:

<%= Html.CheckBox("myCB" )%>

How can I add an onChange handler to this that does a submit?

like image 886
chris Avatar asked Nov 23 '09 16:11

chris


2 Answers

Add an onClick handler to the CheckBox that submits the form the CheckBox belongs to...quick, clickHandler codeless example:

<%= Html.CheckBox("myCB", 
    new { onClick = "$(this).parent('form:first').submit();" });

(example definitely not checked for accuracy)

like image 139
Justin Niessner Avatar answered Oct 23 '22 02:10

Justin Niessner


If you have only one form, and are not using JQuery (you should be, by the way) try this:

<%= Html.CheckBox("myCB", 
new { onClick = "document.form.submit();" });
like image 2
Josh Pearce Avatar answered Oct 23 '22 03:10

Josh Pearce