Alex
Alex

Reputation: 36626

.NET CheckBox control that supports Value property

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?

Upvotes: 2

Views: 9607

Answers (8)

Geoduck
Geoduck

Reputation: 9009

This is an old question, but things have changed. First, in asp.net 4.6, the value attribute will be set on the checkbox. Few of this answers are relevant to CheckBoxList, which uses the ListItem class to generate checkboxes.

My solution for pre-4.6 is to add a data attribute. This creates a <span> control around the checkbox and label which you can find with javascript.

        foreach( ListItem item in selCheckList.Items)
        {
            item.Attributes["data-role-id"] = item.Value;
        }

Upvotes: 0

xr280xr
xr280xr

Reputation: 13302

You can create your own custom control by overriding CheckBox. Here's an example I just created to add a Value property available server and client side:

public class ValueCheckBox : CheckBox
{
    public string Value
    {
        get;
        set;
    }

    protected override void Render(System.Web.UI.HtmlTextWriter writer)
    {
        writer.AddAttribute("value", this.Value);
        base.Render(writer);
    }
}

Upvotes: 0

Alex
Alex

Reputation: 36626

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()

Upvotes: 0

Spencer Ruport
Spencer Ruport

Reputation: 35117

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.

Upvotes: 2

BenAlabaster
BenAlabaster

Reputation: 39864

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.

Upvotes: 1

Michael Haren
Michael Haren

Reputation: 108366

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.

Upvotes: 0

Alex
Alex

Reputation: 36626

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

Upvotes: 0

EdgarVerona
EdgarVerona

Reputation: 1598

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.

Upvotes: -1

Related Questions