ASP.Net CheckBoxes should be able to have values
The ASP.Net CheckBox and CheckBoxList control don’t allow for the use of the full W3C HTML standard. For some reason Microsoft decided to implement a reduced functionality version that creates more work for the programmer. Come on Microsoft why can’t you follow even the simplest of standards? In this case it would make coding the .Net framework easier for you not to mention us!
The W3C HTML 4.01 specification specifically states for the value attribute to:
– Specify for radio buttons and checkboxes –
W3C HTML 4.01 Specification - INPUT Element
To me this quote reads as a strong recommendation to specify the value attribute rather than leave it up to the browsers default (generally "On"). Microsoft decided to not even allow the value attribute to be set. They went so far as to have a validation message built into Visual Studio that says:
Attribute ‘Value’ is not a valid attribute of element ‘CheckBox’
If you ignore the warning and run the page, the value attribute is missing. I have even tried circumventing the ASP.Net HTML-izer by adding value to the attributes collection with no success. Microsoft intentionally strips it, so I can only assume they have some reasoning behind this behavior and I imagine they will say something about security in defense.
There is one way to circumvent the behavior but it requires not using ASP.Net controls. Simply insert traditional input type=”checkbox” elements into your page and collect their values using Request.Form.
Sub Page_Load ()
Dim value As String
value = Request.Form("chkItem")
End Sub
…
<input type="checkbox" id="chkItem" name="chkItem" />
<label for="chkItem">CheckBox Item Text</label>
I made a big deal out of this issue simply because to me it doesn’t make any sense. You can get around the issue and still use ASP.Net controls but it requires extra coding.
Sub Page_Load ()
Dim value As String
If chkItem.Checked Then
value = "Some Value"
Else
value = "Some Other Value"
End If
End Sub
…
<asp:CheckBox ID="chkItem" Text="CheckBox Item Text" runat="server" />
When using the CheckBoxList control you will notice ASP.Net uses tables to layout the list. To keep it from doing this you simply need to add a few attributes. It should then use much more standards compliant code. I also show how to access which boxes are checked after postback in a CheckBoxList.
Sub Page_Load ()
' Find out which boxes were checked
Dim value As String = ""
For Each item As ListItem In cblTest.Items
If item.Selected Then
value &= item.Value
End If
Next
End Sub
…
<asp:CheckBoxList ID="cblList" RepeatLayout="Flow" RepeatDirection="Horizontal" runat="server">
<asp:ListItem Text="Item 1" Value="1" />
<asp:ListItem Text="Item 2" Value="2" />
<asp:ListItem Text="Item 3" Value="3" />
</asp:CheckBoxList>
As always let me know if you have any questions.
Categories
Recent
- Browser Logo Shootout
- Blackberry Surreal Theme
- Blackberry JDE API - User Interface Field Reference
- Getting started with the Blackberry Java Development Environment (JDE)
- ASP.Net DropDownList annoyance
- Getting a Blackberry Curve
- Mambo to the beat of the internet
- ASP.Net has 9 to 5 appeal
- ASP.Net CheckBoxes should be able to have values
- Time to take a look at Python I guess
- Country list formatted for MySQL import
- Kinetic Typography: typographic treatment of an audio sample
- MSN Video Sucks
- Five things about me
- Blackberry MP3 Player Instructions



Could you let me know how to collect checked values from a checkboxlist and store them in a database? [C#}
I am working on a survey using VS 2005, connected to a MS SQL 2005 database. The survey consists of textbox and checkboxlist. Using a submit button, “SqlDataSource1.Insert(); ” is run. Individual collected fields are matched to their corresponding fields in to be stored in the database.
The checkboxlist look like this…
a
b
c
Could you tell me how to put store these values? (there are fields in the database for these).
Database
ID
Name
Q1a
Q1b
Q1c
I have discovered that you can render the “value” attribute. You add the following code to your inherited checkbox custom control class:
What about the following?
Dim chkTest As New CheckBox
chkTest.InputAttributes(”value”) = “my custom value”
When the page renders, if you look at the source the value attribute is there as declared. I hope this helps.
I agree they should have included it. Easy to get around however:
public class CheckBoxWithValue : CheckBox
{
public string Value
{
get { return ViewState["Value"] as string; }
set { ViewState["Value"] = value; }
}
}
Ryan, your custom class doesn’t work. The value attribute is STILL not rendered in the final HTML on the page. It may be in the viewstate, but the tag doesn’t have it. If I ever find a solution, I’ll post it.
Here’s the solution:
Use the InputAttributes property rather than the Attributes property of the CheckBox class.
cb.InputAttributes["value"] = “foo”;
Works like a charm.
Found this on Dave Parslow’s blog:
http://www.daveparslow.com/2007/08/assigning-value-to-aspnet-checkbox.html
The “Value” attribute shows in my browser.
In my ASPX page:
Rendered HTML:
Family
So, the value shows in HTML for me. I’m using ASP.Net 2.0, and I do get the Validation Warning on Visual Studio. However, I’m ignoring it, as the attribute is rendered. Just wanted to share.
There is a similar problem with the name attribute. I’d like to render checkbox controls for use with client side scripts.
Although ChkBox.InputAttributes["name"] = “customName”; does add the name attribute to the HTML page. ASP makes this usless as it also puts in it’s duplicate name attribute.
You know the checkboxlist, well, even though the values don’t render on the page, you can still access them from the code on the serverside using asp.net, because you can select more than one value from a checkboxlist, there cannot be a single SelectedValue, somewhere along the way Microsoft stuffed up there inheritance, or thought it would be okay to leave it as is (Which, it is, really…)
You can get around this quite simply by iterating through the checkboxlist as follows.
for (int i = 0; i < cbo.Items.Count; i++)
{
if (cbo.Items[i].Selected)
{
cbo.Items[i].Value)
}
}
To locate the control on the page I’ve used a recursive script that goes through all the controls within a placeholder, (Or the whole page if you want), then if it finds a checkbox list, it classifies it and uses the above code to obtain the values.
Any questions contact me info at noirenex dot com
I think Ryan’s custom class is the way to go, and as heared 100 times before we can just use InputAttributes to render the property to html:
public class CheckBoxWithValue : CheckBox
{
public string Value
{
get { return ViewState["Value"] as string; }
set { ViewState["Value"] = value; }
}
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
this.InputAttributes["value"] = this.Value;
}
}
I managed to sort this out by using the CheckedChanged event handler. All you need to do is programmatically change the checked value, then you can reference the checkbox.checked value directly because it fires before the page load event
like so….
protected void yourcheckbox_CheckedChanged(object sender, EventArgs e)
{
if (yourcheckbox.Checked == true)
{
yourcheckbox.Checked = false;
}
else
{
yourcheckbox.Checked = true;
}
}
Hope this helps. :-)
I found an almost decent way of doing this:
Declare your checkbox:
Then you can programatically add any values by looping through your controls, looking for checkboxes, and doing this:
Control.InputAttributes.Add(”value”, Control.Attributes(”value”))
It will complain that value is not a valid attribute on the asp:checkbox tag, but it works.
Sorry, it stripped out my asp code:
asp:CheckBox runat=”server” value=”04″ id=”control”