dweremeichik
dweremeichik

Reputation: 61

FindControl wont work on dynamic check boxes

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

Answers (4)

Rezler
Rezler

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

hungryMind
hungryMind

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

JBrooks
JBrooks

Reputation: 10013

Do you have runat="server" when you create the checkboxes?

Upvotes: 0

Brian Mains
Brian Mains

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

Related Questions