11

There isn't the attribute Value :

<asp:CheckBox ID="CheckBox1" runat="server" />

while on standard HTML this is allowed :

<input type="checkbox" ID="CheckBox1" value="My Valyue" />

why?

markzzz
  • 47,390
  • 120
  • 299
  • 507

2 Answers2

28

The Text property is used to render a label for the checkbox.

The control has an InputAttributes property that you can add to:

myChk.InputAttributes.Add("value", "My Value");

I believe that if you simply add the value attribute to the markup, this will also get populated.

You can access the value like so:

myChk.InputAttributes["value"];

To answer the question of why Value is not a build in attribute to the CheckBox control:

A CheckBox in isolation (just by itself) needs no value. By definition it is a boolean and is identified by its ID. All you need to do is check whether it was checked or not.

The value comes into play when you group checkboxes and there is a control for that - the CheckBoxList that uses ListItem - each ListItem does have a Value property.

Jazcash
  • 3,145
  • 2
  • 31
  • 46
Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • that seems odd, i get it for other controls but Checkboxes? Value seems to make more sense. I guess its all about inheritence from a GenericElement and the like for iteration across common interfaces. – j_mcnally Mar 07 '12 at 10:00
  • 2
    NO! Text property is supposed to be the "label" near the checkbox, not the value of the checkbox itself.. – markzzz Mar 07 '12 at 10:05
  • @MrLister - If you have _several_ checkboxes with the same _name_ attribute (making them part of a group), how would you distinguish them? This attribute has been part of checkboxes for a long time. – Oded Mar 07 '12 at 10:13
  • @MrLister - Not at all. We sometimes forget that checkboxes can be grouped this way... – Oded Mar 07 '12 at 10:22
  • Yeah, but I'm supposed to know things like that; to be familiar with HTML. And of course the `value` of a checkbox gets posted when a form is submitted, anyone can see that! – Mr Lister Mar 07 '12 at 10:25
  • Yeah, this is a great workaround! But my question is : why .NET doesnt have `value` attribute? :) – markzzz Mar 07 '12 at 10:27
  • @markzzz - Well... something to ask the creators of the controls. Chances are that they has the same reasoning that MrLister gave, namely that a checkbox in isolation needs no value. By definition it is a boolean and is identified by its ID. The value comes into play when you _group_ checkboxes and guess what, there is a control for that - the [`CheckBoxList`](http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.checkboxlist.aspx). – Oded Mar 07 '12 at 10:31
3

Instead of using the asp:CheckBox control, use the html input checkbox, and run it at the server.

<input type="checkbox" id="ck" runat="server" value='<%# Eval("Value") %>' />
<asp:Label ID="lbl" runat="server" AssociatedControlID="ck" Text='<%# Eval("Name") %>'></asp:Label>

Now you can reference it from codebehind as an HtmlInputCheckBox (my latest example is inside a repeater, so I can decorate this substitute for a checkbox list with other elements, like a tool tip image).

foreach (RepeaterItem repeaterItem in repCheckboxes.Items)
{
    HtmlInputCheckBox listItem = (HtmlInputCheckBox)repeaterItem.FindControl("ck");
    if (listItem.Checked)
    {
         string val = listItem.Value;
         ...

I know this does not answer the "why" of the OP, but this comes up high in searches for this exact problem, and this is a good solution. As for why, I think MS goofed by leaving it out, since you don't have control over the html in a CheckBoxList

user1689571
  • 495
  • 1
  • 4
  • 17