The CheckBox is dead. Long live the CheckBox!

One of the most hated dreaded slightly annoying controls featured in the arsenal is the CheckBox. Microsoft, for some reason unbeknownst to me, decided we don’t need our CheckBoxes to have value. Luckily, there is a quick and lazy way of giving value back to your CheckBox.

Actually, there are many ways of doing it, and one of those ways is very proper in implementation and design… the following is not that way. Again, this is quick and for lack of a better word, lazy.

WebControls have an Attribute property which is a “collection of arbitrary attributes of that control (for rendering only) that do not correspond to properties on that control”. That’s a lot of words to say you can put stuff in the tag that the control doesn’t already have dibs on. For instance:

<asp:CheckBox id=myCheckbox value="I have value!" Text="Tick me" runat="server"></asp:CheckBox>

Perfectly valid, and though “value” isn’t a property of the CheckBox control, it is an attribute that we can access with our code like so:

string value = myCheckbox.Attributes["value"];

And tada, the CheckBox now has value! Okay, go forth and code.

The end.