Friday, October 21, 2016

Historical tech post: Bugs I have seen - ASP.NET checkbox controls (01 Nov 2006)

I have run into a couple of issues with ASP.NET CheckBox server controls. When a CheckBox is nested inside an ItemTemplate inside a Repeater control, the CheckedChanged event doesn’t fire for me. But let's say the Text of each CheckBox is set like this:

    Text='<%# DataBinder.Eval(Container, "DataItem.columnX")%>’ CssClass="hideme"

 where the hideme CSS class looks like:

 <style>.hideme LABEL { DISPLAY: none }</style>

The result is that each checkbox can have a unique Text property, and the CSS class makes it invisible, so it's essentially a custom control property. Then do:

 foreach (RepeaterItem dataItem in RepeaterDataTable.Items)
    CheckBox itemCb = (CheckBox) dataItem.FindControl("CheckBox1");

The loop gets at each CheckBox and you can then see the Text property.
Also, an onclick property can be specified for a CheckBox, but the CheckBox server control has no such documented property. Microsoft documents that arbitrary attributes can be applied to the rendered HTML control. For example, a CheckBox is rendered as an HTML input control of type checkbox. A CheckBox can be created to call a JavaScript function, client-side, by adding the following line to a code-behind method:
    cb1.Attributes.Add("onclick", "jsFunc();");
The CheckBox will also invoke server-side methods as usual.

1 comment: