I finally have the answer I've been looking for!
The asp.net CheckboxList control does in fact render the value attribute to HTML - it has been working for me in a Production site for over a year now! (We ALWAYS have EnableViewState turned off for all our sites and it still works, without any tweaks or hacks)
However, all of a sudden, it stopped working one day, and our CheckboxList checkboxes no longer were rendering their value attribute in HTML! WTF you say? So did we! It took a while to figure this out, but since it had been working before, we knew there had to be a reason. The reason was an accidental change to our web.config!
<pages controlRenderingCompatibilityVersion="3.5">
We removed this attribute from the pages configuration section and that did the trick!
Why is this so? We reflected the code for the CheckboxList control and found this in the RenderItem method:
if (this.RenderingCompatibility >= VersionUtil.Framework40)
{
this._controlToRepeat.InputAttributes.Add("value", item.Value);
}
Dearest dev brothers and sisters do not despair! ALL the answers I searched for here on Stackexchange and the rest of the web gave erroneous information! As of .Net 4.0 asp.net renders the value attribute of the checkboxes of a CheckboxList:
<input type="checkbox" value="myvalue1" id="someid" />
Perhaps not practically useful, Microsoft gave you the ability to add a "controlRenderingCompatibilityVersion" to your web.config to turn this off by setting to a version lower than 4, which for our purposes is completely unnecessary and in fact harmful, since javascript code relied on the value attribute...
We were getting chk.val() equal to "on" for all our checkboxes, which is what originally alerted us to this problem in the first place (using jquery.val() which gets the value of the checkbox. Turns out "on" is the value of a checkbox that is checked... Learn something every day).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…