Hi all, I'm currently stuck with a simple problem, which I cannot seem to get it to work for the past few days.

I'm trying to get the CheckBox type from a ControlCollection and it is displayed within a Table

However, it don't seem to work regardless of how I been trying it...

The checkboxes are created based on the number of records returned from my dataset...

I'm currently clueless as to how I should go about solving this problem...

Below are the codes :

protected void subCheckAll(object sender, EventArgs e)
 {
       checkAllHTML(this, ((CheckBox)sender).Checked);  
 }
private void checkAllHTML(Control parent, bool blnChecked)
    {
        foreach (Control child in parent.Controls)
        {
            if (child is CheckBox)
            {
                CheckBox chk = (CheckBox)child;
                chk.Checked = blnChecked;
                //((CheckBox)child).Checked = blnChecked;
            }
            if ((child.Controls.Count > 0))
            {
                checkAllHTML(child, blnChecked);
            }
        }
    } // Error - Control child is always LiteralControl 
    // I tried casting it as a checkbox but it don't work as well...
private void CreateCheck()
    {
        //Put user code to initialize the page here
        CheckBox chkBox = new CheckBox();
        int i = 0;
        DataSet ds = new DataSet();
        int lintHeadID = 0;
        string lstrGroup = string.Empty;
        bool lblFisrt = false;
        ds = CreateDataSource();
        lstrGroup = "";
        lblFisrt = false;
        lintHeadID = 0;
        for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
        {
            if (lstrGroup != ds.Tables[0].Rows[i][3].ToString()) // Rows[i][3] <-- The [3] The 3rd Column in the Table
            {
                lintHeadID = lintHeadID + 1;
                lstrGroup = (string)ds.Tables[0].Rows[i][3];

                string sHeaderID = lintHeadID + "00";
                chkBox.ID = String.Format(sHeaderID) + "chkHeader" + i;
                //chkBox.ID = Strings.Format(lintHeadID, "00") + "chkHeader" + i;
                chkBox.Text = lstrGroup;

                chkBox.Font.Bold = true;
                chkBox.Font.Name = "Verdana";
                chkBox.Font.Size = FontUnit.XSmall;
                chkBox.Width = Unit.Percentage(100);
                chkBox.BackColor = Color.SkyBlue;
                chkBox.Font.Underline = true;
   
                TableRow tr1 = new TableRow();

                TableCell td3 = new TableCell();
                TableCell td1 = new TableCell();
                td3.Width = Unit.Percentage(5);
                tr1.Cells.Add(td3);
                td1.Controls.Add(chkBox);
                tr1.Cells.Add(td1);
                lblFisrt = !lblFisrt;
                if (lblFisrt)
                {
                    tblPermission.Rows.Add(tr1);
                }
                else
                {
                    tblPermission2.Rows.Add(tr1);
                }
                chkBox.AutoPostBack = true;
                chkBox.CheckedChanged += Master_CheckedChanged;
            }
            chkBox = new CheckBox();
            string sHeadID = lintHeadID + "00";
            chkBox.ID = String.Format(sHeadID) + "chkDetail" + i;
            chkBox.Text = (string)ds.Tables[0].Rows[i][1];
            chkBox.ToolTip = (string)ds.Tables[0].Rows[i][0];
            //store permission id in tooptip


            TableRow tr = new TableRow();
            TableCell td = new TableCell();
            TableCell td2 = new TableCell();

            td2.Width = Unit.Percentage(5);
            tr.Cells.Add(td2);
            td.Controls.Add(chkBox);
            tr.Cells.Add(td);
            chkBox.CheckedChanged += Detail_CheckedChanged;
            if (lblFisrt)
            {
                tblPermission.Rows.Add(tr);
            }
            else
            {
                tblPermission2.Rows.Add(tr);
            }
        }   
    }
<table width="100%" cellpadding="3">
																<tr>
																	<td valign="top" width="48%">
																		<asp:table id="tblPermission" runat="server" Width="333px" BorderWidth="0px" Height="32px">
																			<asp:TableRow>
																				<asp:TableCell BackColor="#CCCCCC" ColumnSpan="2" Text="">
																					<asp:CheckBox AutoPostBack="True" OnCheckedChanged="subCheckAll" ID="chkAll" Visible="True" Text="Select All"
																						Font-Bold="True" Runat="server"></asp:CheckBox>
																				</asp:TableCell>
																			</asp:TableRow>
																		</asp:table></td>
																	<td valign="top" width="48%">
																		<asp:table id="tblPermission2" runat="server" Width="296px">
																			<asp:TableRow>
																				<asp:TableCell BackColor="#CCCCCC" ColumnSpan="2" Text=" " Height="20px"></asp:TableCell>
																			</asp:TableRow>
																		</asp:table></td>
																</tr>
															</table>

I really would appreciate for any assistance on this problem...

I referred to the following post : http://www.c-sharpcorner.com/UploadFile/sushmita_kumari/Checkbox201112006232423PM/Checkbox2.aspx

It just don't seem to work as well....

I been trying other ways but just can't seem to get the child control as a checkbox based on the parent...

Thanks alot in advance

I can't see why it would'nt work.
Compare with one of my methods for clearing all labels.

private void ClearAll(Control parent)
{
    foreach (Control ctl in parent.Controls) {
        if (ctl is Label) {
            if (((Label)ctl).Text == "a") ((Label)ctl).Text = ""; 
        }
        if (ctl.HasControls) ClearAll(ctl); 
    }
}

However, have you considered that your checkboxes may not have been completely rendered before you call the checkAllHTML method?
If you create the checkboxes on the Page_Load event, and then call the checkAllHTML on a postback, they may no longer exist.
I noticed that you haven't enabled ViewState for your checkboxes.
You could try if that helps.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.