Hubert Ee

Hubert Ee

  • NA
  • 4
  • 0

Getting CheckBox in ControlCollection

Dec 7 2009 9:37 PM
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 but 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...

 protected void subCheckAll(object sender, EventArgs e)
{
checkAllHTML(this, ((CheckBox)sender).Checked);
}
// This is the Event of the CheckBox

  private void checkAllHTML(Control parent, bool blnChecked)
{
string sParentTest = parent.Controls.GetType().ToString();

string sParentTest2 = parent.GetType().ToString();

//string sParentTest3 = Parent.Controls.GetType().ToString();

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);
}
}
} // I create the checkboxes based on the results returned from my Dataset


I really would appreciate for any assistance on this problem... 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

Hubert

Answers (1)