I have a web flow (asp.net) which has a drop down and a check box.
When the check box is ticked, I need to disable some fields in that form. When a specific value is selected from the check box, I need to disable other fields.
I specify the checkbox like this:
<%=Html.CheckBox("IsResponseUnavailable", Model.IsResponseUnavailable)%>
And the drop down like this:开发者_StackOverflow中文版
<%= 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've heard about auto-postback - but unsure how to use it - any advice would be great!
I'm using ASP.NET MVC 3.
Thanks! - L
<%= 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();" }) %>
It depends on what you want. Do you want a postback to the server where the server will redraw the view with the correct changes? Or do you want javascript to run when the box is ticked so that the correct fields are changed? Javascript is much smoother and easier to do. There really isn't a way to do an auto-postback without some javascript. Dennis' answer is as basic as it gets and it still uses javascript.
It sounds like you might be better off with webforms instead of MVC if you are doing most of your logic during postbacks. Otherwise I'd try to enrich your UI a bit with some JQuery and take advantage of MVC since you're using it.
You could 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 was trying to re-render the HTML for server side then you could take a look at the jQuery load() function
You can also auto submit a form with jQuery using this method but I don't think that's what you want to do.
精彩评论