dimanche 15 novembre 2015

Get checkbox items - Unable to cast object of type 'System.Web.UI.WebControls.CheckBoxList' to type 'System.Web.UI.HtmlControls.HtmlInputCheckBox'

I have this repeater that generates checkboxes and labels.

 <asp:Repeater ID="Repeater1" runat="server" 
        onitemcommand="Repeater1_ItemCommand">
   <ItemTemplate>
     <table>
        <tr>
            <td>  <asp:CheckBoxList ID="CheckBoxList1" runat="server">
       <asp:ListItem></asp:ListItem></asp:CheckBoxList>
            </td>
              <td> <asp:Label ID="lblTest" runat="server" Text='<%# Eval("MyColumn") %>'></asp:Label>
            </td>
     </table>    </ItemTemplate> </asp:Repeater>

When I try to see if one is checked, it says the checkbox does not exist. How do I get access to it? I also tried Repeater1.CheckBoxList1 but that didn't work either. I will be using a Submit button to gather info.

if (CheckBoxList1.Checked = True) {

                        }

I have tried this solution (from here: Get values of all checkboxes in repeater) ...

foreach (RepeaterItem aItem in Repeater1.Items)
                {
                    HtmlInputCheckBox CheckBoxList1 = (HtmlInputCheckBox)aItem.FindControl("CheckBoxList1");
                    if (CheckBoxList1.Checked)
                    {
                        //HERE IS YOUR VALUE: CheckBoxList1.Value
                        sqlCmd2.CommandText = string.Format("UPDATE FormField SET Visible = 0");
                        sqlCmd2.ExecuteNonQuery();
                    }
                }

But I get this error: Unable to cast object of type 'System.Web.UI.WebControls.CheckBoxList' to type 'System.Web.UI.HtmlControls.HtmlInputCheckBox'.




Aucun commentaire:

Enregistrer un commentaire