Reputation: 61
Find control doesn't seem to work with my dynamic check boxes; what i am trying to do is see if a user has checked a check box, eventually which ones as well.
In my code i have a test to see if it is working properly
public void test()
{
// Find control on page.
CheckBox myControl1 = (CheckBox)Table1.FindControl("CBX0");
if (myControl1 != null)
{
// Get control's parent.
Control myControl2 = myControl1.Parent;
Response.Write("Parent of the text box is : " + myControl2.ID);
if (myControl1.Checked == true)
{
Response.Write("check box checked");
}
}
else
{
Response.Write("Control not found");
}
}
when i run my code it does print "parent of the text box is", however it will not print the parent, which is supposed to be mycontrol2.id
Upvotes: 0
Views: 484
Reputation: 1074
Perhaps the parent control doesn't have an ID.
Instead try:
Response.Write("Parent of the text box is : " + myControl2);
to find out the type of the Parent. I think if you're expecting the parent to be the Table you're wrong. It will probably be a TableCell.
Upvotes: 1
Reputation: 6999
When are you calling this test method. Consider calling it onPreRender and it should return the ID. When you dynamically create control and add to page, and if don't assign nny ID, .net assign them before rendering to browser
Upvotes: 0
Reputation: 50728
myControl2 is probably higher up in the hierarchy; keep drilling up the hierarchy to find the control, it may be mycontrol1.Parent.Parent
or more parent references. ASP.NET sometimes puts other controls in the hierarchy and so it may not be a direct parent.
HTH.
Upvotes: 0