I had recently moved a “DataList” control in to a UserControl and referenced it on my ASPX page. The DataList contains checkboxes with checked properties assigned by the data source initially.
<asp:DataList ID="dlspec" CssClass="specs" runat="server" GridLines="Vertical" OnItemDataBound="dlspec_ItemDataBound">
<FooterStyle BackColor="#CCCCCC" />
<AlternatingItemStyle CssClass="alt-grey" />
<SelectedItemStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
<ItemTemplate>
<table>
<tr>
<td class="leftcol">
<asp:Label ID="lblDimension" runat="server" Text='<%# Eval("Dimension") %>'></asp:Label>:
</td>
<td class="ProductDetailData">
<asp:Label ID="lblName" runat="server" Text='<%# Eval("Attribute") %>'></asp:Label>
</td>
<td class="find-similar">
<asp:CheckBox ID="FindSimilarCheckbox" runat="server" Checked='<%# Eval("CheckBox")=="true"? true:false %>' Text='<%# Eval("AttributeID") %>' Visible='<%# Eval("CheckBoxState")=="0"? true:false %>' />
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
Now, on a button click event in the “aspx” to which the user control is bound to, i try to get the “checked” properties of the check boxes to go through some logic.
I basically use the below to find the usercontrol and loop through the controls in it.
Control SpecsPanel = FindSimilarPnl.FindControl("Specifications").FindControl("dlspec");
foreach (Control ct in SpecsPanel.Controls)
GetCheckedAttributes(ct, ref qry);
However the “checked’ property of the checkboxes always comes out to be “false” after i moved the datalist in to the user control. Any ideas why? Am I missing something silly? Greatly appreciate any thoughts ideas. Let me know if I need to add more code for you to understand better.
Thanks
I found out why this is happening…going to answer my own question.
So the CheckBox ID in the template is “FindSimilarCheckBox” and it is renamed at data-bind time. so when the postback occurs, the server returns the ID’s for all the checkboxes as “FindSimilarCheckBox” and Checked property for everything is false. I had to re-data bind the usercontrol and this time put in a condition to check if it is a postback operation and if the Check Box’s Unique ID exists in the Request.Form collection in order to set the Checked property on the chkbox. Something like this :
My issue is solved now. Hope this answer helps you understand what caused my issue. Let me know if i need to give more detail.