views:

968

answers:

6

I am aware that the default .NET CheckBox control doesn't support the Value property. This seems weird to me because the input control does support value as part of the html spec.

So my question is whether or not anyone here knows of a custom user control out there for asp.net that acts similar to the standard ASP .NET CheckBox and CheckBoxList?

A: 

Isn't "Checked" the same as "Value"?

http://www.w3schools.com/aspnet/control_checkbox.asp

Take a look in there. You don't really need a property specifically named "Value", because "Checked" will give you the same information.

EdgarVerona
A: 

No they are not the same

The feature I am working on requires a list of product upgrades to be shown as a list of CheckBoxes. Based on which checkboxes are "checked" i will need to add them to the product. The items in the CheckBoxList are dynamic so when i iterate over the checked items I need some sort of unique identifier. The "Checked" value is only a Boolean

Alex
It should still have an ID, you could use that property.
EdgarVerona
A: 

What MS has done here is created their ASP controls with a .net feel to them. However, since they're supposed to work with browsers, they render as standard HTML controls.

So in the code-behind cs/vb, you see .Checked (bool), while in the client/javascript you see .value.

If what you want is a unique identifier, then you need to be looking at ID or ClientID. Alternatively, you could add an attribute to the checkboxt (.Attributes.Add()) and use that.

Michael Haren
If you need a link, here:http://www.daveparslow.com/2007/08/assigning-value-to-aspnet-checkbox.html
EdgarVerona
A: 

If you only need access to it on the server side, you can easily create a server control that inherits the Checkbox control and add a property to it called value which can be set and retrieved from the server side.

If you need to access it from the client side however, you're gonna have to get imaginitive with what the server control renders - likely a combination of a checkbox and hidden field whereby the hidden field contains the value.

If all you're doing is extracting the value of the checked boxes on the server side, I would imagine that a simple extension of the checkbox control would suffice though - add an extra property that can be set and retrieved from the server side... walk in the park.

BenAlabaster
A: 

Perhaps I'm misunderstanding your question but are you aware that you can use the typical HTML controls as well?

For example create an ASPX page and add the following source:

    <div>
            <input type="checkbox" runat="server" id="chkBox" value="test" />
            <asp:Label ID="lblCheckboxValue" runat="server" />
    </div>

    <asp:Button runat="server" OnClick="Button_Click" />

Then in your code behind add the following code:

protected void Button_Click(object sender, EventArgs e)
        {
            if (chkBox.Checked)
                lblCheckboxValue.Text = chkBox.Value;
            else
                lblCheckboxValue.Text = "";

        }

You can set the html check box value property to whatever you like.

Spencer Ruport
A: 

It seems that the values are not rendered in the html but are still stored in the viewstate. So i was able to pull the value on the server side when i accessed the ListItem object in the CheckboxList.Items()

Alex