How to use automatic postback with checkboxes and dropdowns?

I have a web stream (asp.net) that has a drop-down list and a checkbox.

When a checkmark is checked, I need to disable some fields in this form. When a certain value is selected from this checkbox, I need to disable other fields.

I check the box:

<%=Html.CheckBox("IsResponseUnavailable", Model.IsResponseUnavailable)%> 

And like this:

 <%= Html.MyDropDownList(string.Format("Questions[{0}].Answer", i), (IEnumerable<SelectListItem>)ViewData["Periods"], Model.Questions[i].Answer)%> 

Where MyDropDownList is an extension of Html.DropDownList

I heard about auto-reverse gear - but don't know how to use it - any advice would be great!

I am using ASP.NET MVC 3.

Thanks! - L

+4
source share
3 answers
 <%= Html.CheckBox("IsResponseUnavailable", Model.IsResponseUnavailable, new { onClick = "this.form.submit();" }) %> <%= Html.MyDropDownList(string.Format("Questions[{0}].Answer", i), (IEnumerable<SelectListItem>)ViewData["Periods"], Model.Questions[i].Answer), new { onchange = "this.form.submit();" }) %> 
+10
source

It depends on what you want. Do you want to receive a postback to the server on which the server will redraw the view with the correct changes? Or do you want javascript to start when the field is checked to change the correct fields? Javascript is much smoother and easier to use. There really is no way to do auto-postback without any javascript. Dennis's answer is as simple as he is, and he still uses javascript.

It looks like you might be better off with webforms instead of MVC if you execute most of your logic during postback. Otherwise, I will try to enrich your interface a little with some jQuery and use MVC, since you use it.

+3
source

You can just do this client side using a bit of jQuery, something like

 $('#IsResponseUnavailable').change(function() { if ($(this).has('[checked]')) { $('#idOfElement').attr('disabled', 'disabled'); } else { $('#idOfElement').removeAttr('disabled'); } }); 

If you tried to re-render the HTML for the back end, you can take a look at the jQuery load () function

You can also automatically submit a form using jQuery using this method , but I don't think you want to do.

+1
source

Source: https://habr.com/ru/post/1342128/


All Articles