One interesting phenomenon I've noted today is that if a checkbox input is not checked, it is not included in the postback开发者_高级运维 variables; no "_xyzcheckbox : off" corresponds with the "_xyzcheckbox: on" alternative (when 'checked').
This leads to an issue of storing check state if the checkbox is hidden on the server and then revealed later; the Checkbox does not know whether or not it is simply hidden (not in the DOM) or if it is being unchecked based on the postback that the browser sends back to the server, as the postback includes no reference to the checkbox if it is unchecked.
I am having an issue where I need to hide a checkbox (using .Visible = false on a parent of the checkbox) and show it depending on the users input, but the state cannot be persisted because of the aforementioned issue.
Has anyone else come across this issue? How have you solved it or gotten around it?
Note: The most obvious answer his hiding the checkbox on the client (setting display: none), this isn't an option though as we're trying to reduce the markup on the page so that postbacks are smaller.
Thanks for the feedback thus far.
Thanks :)
Rather than use .Visible (which removes the checkbox from the emitted markup, and therefore no chance of it being in a POST back), why not apply a CSS class to it?
The CSS could be as follows:
input.hidden {
display: none;
}
In your ASP.NET page simply set the CssClass property:
MyCheckbox.CssClass = "hidden";
And you should be fine.
In the mark-up, you can default selected = "false"
.
In the code-behind, you can determine the state by mycheckbox.checked
, which will either return true
or false
.
精彩评论